[Fixed] Object of Type int64 Is Not JSON Serializable

Object of Type int64 Is Not JSON Serializable

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.

Output:

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.

Output:

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.

Output:

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”. You can observe this in the following example. 

Output:

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.

Output:

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:

Output:

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.

Output:

While using an encoder class, you can even convert collection objects containing non-serializable data types to JSON very easily as follows.

Output:

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.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning! 

Was this post helpful?


import_contacts

You may also like:

Related Posts

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.