I have a basic dict as follows:
sample = {}
sample['title'] = "String"
sample['somedate'] = somedatetimehere
When I try to do jsonify(sample) I get:
TypeError: datetime.datetime(2012, 8, 8, 21, 46, 24, 862000) is not JSON serializable
What can I do such that my dictionary sample can overcome the error above?
Note: Though it may not be relevant, the dictionaries are generated from the retrieval of records out of mongodb where when I print out str(sample['somedate']), the output is 2012-08-08 21:46:24.862000.
Updated for 2018
The original answer accommodated the way MongoDB “date” fields were represented as:
{"$date": 1506816000000}If you want a generic Python solution for serializing
datetimeto json, check out @jjmontes’ answer for a quick solution which requires no dependencies.As you are using mongoengine (per comments) and pymongo is a dependency, pymongo has built-in utilities to help with json serialization:
http://api.mongodb.org/python/1.10.1/api/bson/json_util.html
Example usage (serialization):
Example usage (deserialization):
Django
Django provides a native
DjangoJSONEncoderserializer that deals with this kind of properly.See https://docs.djangoproject.com/en/dev/topics/serialization/#djangojsonencoder
One difference I’ve noticed between
DjangoJSONEncoderand using a customdefaultlike this:Is that Django strips a bit of the data:
So, you may need to be careful about that in some cases.