Categories
Python Answers

How to fix TypeError: ObjectId(”) is not JSON serializable with Pymongo?

Spread the love

Sometimes, we want to fix TypeError: ObjectId(”) is not JSON serializable with Pymongo.

In this article, we’ll look at how to fix TypeError: ObjectId(”) is not JSON serializable with Pymongo.

How to fix TypeError: ObjectId(”) is not JSON serializable with Pymongo?

To fix TypeError: ObjectId(”) is not JSON serializable with Pymongo, we can use the json_util module from Pymongo.

For instance, we write

def parse_json(data):
    return json.loads(json_util.dumps(data))

to create the parse_json function.

that calls json_util.dumps on the data returned by Pymongo.

Then we call json.loads to parse the JSON string returned as a dict.

Conclusion

To fix TypeError: ObjectId(”) is not JSON serializable with Pymongo, we can use the json_util module from Pymongo.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *