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.