Table of Contents
JSON objects are used extensively in data transmission or in data interchange between different languages. To transmit data, we often convert the data to JSON objects. After that, we transmit the data. Sometimes, while creating a JSON object, your program might run into the TypeError exception with the message “object of type int64 is not json serializable”. IN this article, we will discuss how we can avoid this error.
How Do We Create JSON Objects in Python?
In python, we create a JSON object using the dumps()
method defined in the json module. The dumps()
method takes a python object as its input argument and returns a JSON string after execution. You can observe this in the following example.
1 2 3 4 5 6 7 8 9 10 |
import json x = 1117 print("The input value is:", x) print("Data type of input is:", type(x)) y = json.dumps(x) print("The output value is:", y) print("Data type of output is:", type(y)) |
Output:
1 2 3 4 5 6 |
The input value is: 1117 Data type of input is: <class 'int'> The output value is: 1117 Data type of output is: <class 'str'> |
In the output, you can observe that the input number is converted into a json string by the dumps()
method. You can also convert a collection object like a list or dictionary to json string using the dumps()
method as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import json myList = [1, 2, 3, 4, 5, 6, 7] print("The input list is:", myList) print("Data type of input is:", type(myList)) y = json.dumps(myList) print("The output value is:", y) print("Data type of output is:", type(y)) myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49} print("The input dictionary is:", myDict) print("Data type of input is:", type(myDict)) y = json.dumps(myDict) print("The output value is:", y) print("Data type of output is:", type(y)) |
Output:
1 2 3 4 5 6 7 8 9 10 |
The input list is: [1, 2, 3, 4, 5, 6, 7] Data type of input is: <class 'list'> The output value is: [1, 2, 3, 4, 5, 6, 7] Data type of output is: <class 'str'> The input dictionary is: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49} Data type of input is: <class 'dict'> The output value is: {"1": 1, "2": 4, "3": 9, "4": 16, "5": 25, "6": 36, "7": 49} Data type of output is: <class 'str'> |
We also use the dump()
method to create a JSON object from a python object. The dump()
method takes the python object as its first argument and a file object as its second input argument. After execution, it creates a JSON object and appends it to the file that is represented by the file object. You can observe this in the following example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import json myFile = open("sample.txt", "w") myList = [1, 2, 3, 4, 5, 6, 7] print("The input list is:", myList) print("Data type of input is:", type(myList)) json.dump(myList, myFile) myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49} print("The input dictionary is:", myDict) print("Data type of input is:", type(myDict)) json.dump(myDict, myFile) myFile.close() # read the content after saving the file. myFile = open("sample.txt", "r") content = myFile.read() print("The content in the file is:") print(content) |
Output:
1 2 3 4 5 6 7 8 |
The input list is: [1, 2, 3, 4, 5, 6, 7] Data type of input is: <class 'list'> The input dictionary is: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49} Data type of input is: <class 'dict'> The content in the file is: [1, 2, 3, 4, 5, 6, 7]{"1": 1, "2": 4, "3": 9, "4": 16, "5": 25, "6": 36, "7": 49} |
You can observe that the output by the dumps()
method is similar to the dump() method. However, the dump() method saves the output in a file and the dumps()
method returns the output as a string.
What Is JSON Serializable Object?
In python, not all data types can be converted into JSON format. For instance, we can only convert the data types including int, dict, list, str, int, float, and the values True, False, and None to JSON format.
Objects with any other datatype are not json serializable and cannot be converted to JSON format.
Typeerror: Object of Type int64 Is Not Json Serializable- Causes
When we pass an object of the data type that is not serializable to the dump()
or dumps()
method, the program runs into the TypeError
exception. The int64
data type is not a serializable data type and when we try to pass it to the dumps()
method, the program runs into the TypeError exception and shows the message “object of type int64 is not json serializable”.
1 2 3 4 5 6 7 8 9 10 11 |
import json import numpy x = numpy.int64(1117) print("The input value is:", x) print("Data type of input is:", type(x)) y = json.dumps(x) print("The output value is:", y) print("Data type of output is:", type(y)) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
The input value is: 1117 Data type of input is: <class 'numpy.int64'> Traceback (most recent call last): File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 7, in <module> y = json.dumps(x) File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps return _default_encoder.encode(obj) File "/usr/lib/python3.8/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/usr/lib/python3.8/json/encoder.py", line 179, in default raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type int64 is not JSON serializable |
Even if an integer of the type int64 is present in another object like a dictionary, the TypeError exception will occur with the message “TypeError: Object of type int64 is not JSON serializable”. You can observe this in the following example.
1 2 3 4 5 6 7 8 9 10 11 12 |
import json import numpy x = numpy.int64(1117) myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: x} print("The input dictionary is:", myDict) print("Data type of input is:", type(myDict)) y = json.dumps(myDict) print("The output value is:", y) print("Data type of output is:", type(y)) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
The input dictionary is: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 1117} Data type of input is: <class 'dict'> /usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.7) or chardet (3.0.4) doesn't match a supported version! warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported " Traceback (most recent call last): File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 8, in <module> y = json.dumps(myDict) File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps return _default_encoder.encode(obj) File "/usr/lib/python3.8/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/usr/lib/python3.8/json/encoder.py", line 179, in default raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type int64 is not JSON serializable |
Typeerror: Object of Type int64 Is Not Json Serializable- Solutions
Any data type defined in the numpy module isn’t serializable. So, whenever you are using libraries like scipy, numpy, or pandas, you might run into this error while converting the python objects to JSON objects.
To avoid Typeerror: Object of Type int64 Is Not Json Serializable, you can first convert the numpy objects to normal int and float objects before creating a JSON object.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 |
import json import numpy x = numpy.int64(1117) print("The input value is:", x) print("Data type of input is:", type(x)) y = json.dumps(int(x)) print("The output value is:", y) print("Data type of output is:", type(y)) |
Output:
1 2 3 4 5 6 |
The input value is: 1117 Data type of input is: <class 'numpy.int64'> The output value is: 1117 Data type of output is: <class 'str'> |
Instead of manually converting each python object to the serializable data type, you can define a class that inherits the JSONEncoder
class to convert the numpy objects into serializable objects. After that, you can pass the class to the dump()
method or the dumps()
method using the cls parameter. In this way, whenever a data type such as int64 is encountered, it will be converted to int datatype. Similarly, other numpy objects will be converted to corresponding serializable data types and the program will not run into TypeError exception. You can observe this in the following example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import json import numpy class NpEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, numpy.integer): return int(obj) if isinstance(obj, numpy.floating): return float(obj) if isinstance(obj, numpy.ndarray): return obj.tolist() return super(NpEncoder, self).default(obj) x = numpy.int64(1117) print("The input value is:", x) print("Data type of input is:", type(x)) y = json.dumps(x, cls=NpEncoder) print("The output value is:", y) print("Data type of output is:", type(y)) |
Output:
1 2 3 4 5 6 |
The input value is: 1117 Data type of input is: <class 'numpy.int64'> The output value is: 1117 Data type of output is: <class 'str'> |
While using an encoder class, you can even convert collection objects containing non-serializable data types to JSON very easily as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import json import numpy class NpEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, numpy.integer): return int(obj) if isinstance(obj, numpy.floating): return float(obj) if isinstance(obj, numpy.ndarray): return obj.tolist() return super(NpEncoder, self).default(obj) x = numpy.int64(1117) myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: x} print("The input dictionary is:", myDict) print("Data type of input is:", type(myDict)) y = json.dumps(myDict, cls=NpEncoder) print("The output value is:", y) print("Data type of output is:", type(y)) |
Output:
1 2 3 4 5 6 |
The input dictionary is: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 1117} Data type of input is: <class 'dict'> The output value is: {"1": 1, "2": 4, "3": 9, "4": 16, "5": 25, "6": 36, "7": 1117} Data type of output is: <class 'str'> |
In the output, you can observe that the dictionary containing an integer of type int64 has been converted to JSON without an error. This is due to the reason that we have used the encoder class that converts the integer data types defined in the numpy module to the simple integer type.
Conclusion
In this article, we have discussed how JSON objects are created in python and why the message “TypeError: object of type int64 is not json serializable” is shown. If you are using numpy, scipy, pandas, or any other module that is created on the top of the numpy module, you should use the encoder function that we created in the last example to avoid the TypeError exception.
Further reading:
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!