If you are running a Python project with MongoDB database, you may have encountered the “TypeError: Object of Type ObjectId is Not JSON Serializable” error.
This error usually occurs if you are trying to serialize an ObjectId in JSON format.
ObjectId is a special data type in MongoDB, and it is not directly converted to JSON format.
If you are not familiar with Python or MongoDB, this error can be frustrating and confusing.
However, don’t worry! In this article, we will explain to you the causes of this error.
Also, we will provide some solutions to help you resolve it.
So let’s get started!
Why this error occur?
As I said earlier, this error occurs because when you’re trying to serialize an ObjectId in JSON format.
JSON (JavaScript Object Notation) is a lightweight data format used to exchange data between a server and a client.
In Python, you will be able to use the built-in json module to encode and decode JSON data.
However, if you are trying to encode an ObjectId in JSON format using the json.dumps() method, you will get the following error:
TypeError: Object of type ObjectId is not JSON serializable
This error typically occurs because ObjectId is not a standard Python data type and can’t be directly serialized to JSON format.
What are the causes of the error?
There are multiple reasons why you may get this error:
- Trying to Serialize an ObjectId
- Passing an ObjectId to the json.dumps() Method
- Using a Custom Encoder that Does Not Support ObjectId
Reason 1: Trying to Serialize an ObjectId
ObjectId is a 12-byte BSON type, which means it is not compatible with the JSON data format.
Reason 2: Passing an ObjectId to the json.dumps() Method
Another common reason for this error is passing an ObjectId to the json.dumps() method.
The json.dumps() method is used to encode a Python object into a JSON string.
However, if you are passing an ObjectId to this method, it will raise the TypeError Object of Type ObjectId is Not JSON Serializable error.
Reason 3: Using a Custom Encoder that Does Not Support ObjectId
If you are using a custom JSON encoder which is doesn’t support the ObjectId data type, you may get this error.
A custom encoder is a Python class which used to convert Python objects to JSON format.
When the custom encoder doesn’t support the ObjectId data type, it will raise an error.
How to solve the error?
Now that you know the causes of this error let’s discuss some solutions to help you resolve it.
Solution 1: Converting ObjectId to String
The essential solution to this error is to convert the ObjectId to a string before serializing it to JSON format.
You can do this through calling the str() method on the ObjectId object.
For example:
from bson import ObjectId
import json
obj_id = ObjectId('611c200034ddbc170a16f223')
json_data = json.dumps({'_id': str(obj_id)})
print(json_data)
If you run the code, the output will be:
{“_id”: “611c200034ddbc170a16f223”}
In this example, we first create an ObjectId object and then we will convert it to a string using the str() method.
Then, we use the json.dumps() method to serialize the object to JSON format.
Solution 2: Creating a Custom Encoder that Supports ObjectId
Another solution to this error is to create a custom JSON encoder which is to supports the ObjectId data type.
This way involves creating a Python class that derives from the json.JSONEncoder class.
Then overrides the default() method that will support the ObjectId data type.
For example:
from bson import ObjectId
import json
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, ObjectId):
return str(obj)
return json.JSONEncoder.default(self, obj)
obj_id = ObjectId('611c200034ddbc170a16f223')
json_data = json.dumps({'_id': obj_id}, cls=CustomEncoder)
print(json_data)
When you run the example code, the output will be:
{“_id”: “611c200034ddbc170a16f223”}
In this example, we first determine a custom encoder class called “CustomEncoder“ that inherits from the “json.JSONEncoder” class.
We override the default() method to check if the object is an occurrence of the ObjectId data type.
If it is, we convert it to a string using the “str()“ method. If it is not, we call the “default()” method of the “json.JSONEncoder” class.
Then, we create an “ObjectId” object and use the “json.dumps()” method to serialize it to JSON format.
We pass the custom encoder class as the “cls” parameter to the “json.dumps()” method.
Solution 3: Using the bson.json_util Module
The bson.json_util module is a part of the PyMongo driver for MongoDB.
It provides a set of helper functions and classes for encoding and decoding BSON data types and JSON format.
For example:
from bson import ObjectId
import bson.json_util
# Create an ObjectId
obj_id = ObjectId()
# Convert ObjectId to a JSON string
json_str = bson.json_util.dumps({'_id': obj_id})
# Print the JSON string
print(json_str)
# Convert JSON string back to a dictionary with ObjectId
obj_dict = bson.json_util.loads(json_str)
# Print the dictionary
print(obj_dict)
In this example, we import the ObjectId and bson.json_util modules.
Then, we create an “ObjectId” and convert it to a “JSON“ string using the “bson.json_util.dumps()” function.
Next, we print the JSON string and then convert it back to a dictionary using the “bson.json_util.loads()” function. Finally, we print the dictionary to verify that it contains the “ObjectId“.
Note:
Remember to convert the ObjectId to a string before serializing it to JSON format.
You can do this by calling the str() method on the ObjectId object or by using a custom encoder that supports the ObjectId data type.
Additional Resources
- Typeerror: object of type int64 is not json serializable
- Typeerror: object of type datetime is not json serializable
- typeerror: object of type set is not json serializable
- Typeerror object of type float32 is not json serializable
FAQs
Yes, you can use the json.dumps() method to serialize a MongoDB document that consists of ObjectId.
However, you need to convert the ObjectId to a string before serializing it using one of the solutions mentioned above.
Yes, you can use the json.loads() method to deserialize a JSON string that contains ObjectId.
However, you need to use the bson.json_util module to convert the string back to an ObjectId.
Some other BSON data types that are not compatible with JSON format include ISODate, BinData, and Long.
Conclusion
To conclude, in this article, we discussed the reasons why this error occurs and provided three solutions to help you serialize ObjectId to JSON format.
You can use any of these solutions based on your requirements and preferences.
I hope this article can help you to resolve the TypeError Object of Type ObjectId is Not JSON Serializable error. Happy coding!