Issue
I am trying to return return a list of dictionaries as part of a get request. I'm calling the function below.
def get_data(id):
selected_data = Database.get_date(id)
json.dumps(selected_data, default=str)
return jsonify({'data': selected_data})
selected_data is like
[{'date': datetime.date(2019, 1, 15), 'id': 1, 'name': 'John '}, {'date': datetime.date(2019, 1, 11), 'id': 2, 'name': 'Jane'}]
But, I am getting the error below
TypeError(repr(o) + " is not JSON serializable") TypeError: datetime.date(2019, 1, 15) is not JSON serializable
I've tried using json.dumps like this stack overflow thread suggests but still getting the same error.
Any help is appreciated.
Solution
Try one of the following
selected_data["date"] = selected_data["date"].isoformat()
selected_data["date"] = selected_data["date"].strftime("%Y-%m-%d %H:%M:%S:%f")
selected_data["date"] = str(selected_data["date"])
Answered By - Srinivas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.