Python TypeError: datetime.datetime not JSON serializable while using Mongo

Writing by on Monday, 3 of November , 2014 at 6:18 pm

This post from explains it.

Basically, you will need to use the default=json_util.default argument as defined in the PyMongo Docs

So, while converting a date object in a dictionary to JSON

from bson import json_util
import json
json_str = json.dumps(anObject, default=json_util.default)
print json_str

And while reading back the JSON string into a dictionary

from bson import json_util
import json
json.loads(json_str, object_hook=json_util.object_hook)

Leave a comment

Category: Python

No Comments

No comments yet.

Leave a comment

You must be logged in to post a comment.

Shivdev Kalambi's Blog

Shivdev Kalambi is a Software Development Manager, previously a Principal Software Engineer at ArcSight/HP. With over 16 years' experience in software development, he's worked on several technologies and played different roles and contributed to all phases of projects. Non-tech activies include Ping-pong, Rock Climbing and Yoga at PG, Golf, Skiing, Swimming & a beer enthusiast.