TypeError: Object of type int64 is not JSON serializable – How To Fix It?

TypeError: Object of type int64 is not JSON serializable – How To Fix It?

If you are getting trouble with the “TypeError: Object of type int64 is not JSON serializable”, keep reading our article. We will give you some methods to handle the problem.

Reason for “TypeError: Object of type int64 is not JSON serializable”

Int64 stands for 64-bit integers. It means memory uses 64 bits to present the number from -9223372036854775808 to 9223372036854775807.

In Python, we can only convert a few types into JSON formats, such as int, dict, list, str, int, and float, As a result, you will receive an error when trying to convert other types to JSON format.

For example:

import numpy as np
import json
 
even_num = np.array([0,2,4,6,8])
odd_num = np.array([1,3,5,7,9])
 
myNum = json.dumps({"even": even_num[0], "odd": odd_num[0]})

Result:

TypeError Traceback (most recent call last)
line 7, in <module>
   myNum = json.dumps({"even": even_num[0], "odd": odd_num[0]})
TypeError: Object of type int64 is not JSON serializable

Solution to this problem

Convert the type int64 to the type int

Because the int data type can be converted to json format, we need to convert int64 to int first. Then we can convert to json format without raising errors.

To convert the type int64 to the type int, we use type casting with the syntax:

int(var_name)

Input:

import numpy as np
import json
 
even_num = np.array([0,2,4,6,8])
odd_num = np.array([1,3,5,7,9])
 
# Using type casting to convert int64 to int
myNum = json.dumps({"even": int(even_num[0]), "odd": int(odd_num[0])})
 
print(myNum)

Result:

{"even": 0, "odd": 1}

Custom JSONEncoder class

JSONEncoder class encodes for Python’s data types. They are converted to correspond to JSON type. 

First, we will create a class named JSONEncoder that overrides the default method to convert the type int64 to the type int. This class looks like that:

class JSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.int64):
            return int(obj)
        return json.JSONEncoder.default(self, obj)

Then, we will pass the class to the argument cls of json.dumps() function, Our complete code would look like this:

import numpy as np
import json

class JSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.int64):
            return int(obj)
        return json.JSONEncoder.default(self, obj)
      
even_num = np.array([0,2,4,6,8])
odd_num = np.array([1,3,5,7,9])
 
# Pass the class to the cls argument
myNum = json.dumps({"even": even_num[0], "odd": odd_num[0]}, cls = JSONEncoder)
print(myNum)

Result:

{"even": 0, "odd": 1}

Custom a function for the default argument in json.dumps() function

In the json.dumps() function, there is an argument called default which gets a called function to redefine unsupported type in JSON format.

Code:

def serialise(obj):
    if isinstance(obj, np.int64):
        return int(obj)
      
import json
import numpy as np
 
even_num = np.array([0,2,4,6,8])
odd_num = np.array([1,3,5,7,9])
 
# Pass the serialise function() to the default argument
myNum = json.dumps({"even": even_num[0], "odd": odd_num[0]}, default = serialise)
print(myNum)

Result:

{"even": 0, "odd": 1}

Summary

We have represented you solutions to fix the error “TypeError: Object of type int64 is not JSON serializable” with detailed implementations. Hope our article is helpful for you. Thank you for reading!

Maybe you are interested:

Leave a Reply

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