object is not JSON serializable

While @ErenGüven shows you a nice manual approach to solving this json serializing issue, pymongo comes with a utility to accomplish this for you. I use this in my own django mongodb project:

import json
from bson import json_util

json_docs = []
for doc in cursor:
    json_doc = json.dumps(doc, default=json_util.default)
    json_docs.append(json_doc)

Or simply:

json_docs = [json.dumps(doc, default=json_util.default) for doc in cursor]

And to get them back from json again:

docs = [json.loads(j_doc, object_hook=json_util.object_hook) for j_doc in json_docs]

The helper utilities tell json how to handle the custom mongodb objects.


When you pass db.units.find() to response you pass a pymongo.cursor.Cursor object to json.dumps ... and json.dumps doesn't know how to serialize it to JSON. Try getting the actual objects by iterating over the cursor to get its results:

[doc for doc in db.units.find()]