Select Page

How to Solve Python TypeError: Object of type int32 is not JSON serializable

by | Programming, Python, Tips

This error occurs when you try to convert a numpy.int32 integer to a JSON string using the json.dumps() method. The json.dumps() method can serialize ordinary Python integers. You can solve this error by converting the numpy.int32 number to a Python integer by passing it to the built-in int() function. For example,

json_str = json.dumps(int(numpy_int32))

This tutorial will go through how to solve the error with code examples.


TypeError: Object of type int32 is not JSON serializable

Let’s break up the error message to understand what the error means. TypeError occurs whenever you attempt to use an illegal operation for a specific data type. The part “Object of type int32” tells us the error is due to an illegal operation with a numpy.int32 object.

Serialization in Python refers to converting a Python object into a transmittable format that we can recreate when needed using deserialization. JSON serialization returns a human-readable string form called a JSON string. The JSON encoder json.dump() and json.dumps() can only serialize certain object types like dictionaries, lists, or strings.

is not JSON serializable” informs us that the JSON serialization is an illegal operation for the numpy.int32 type.

Example

Let’s look at an example where we want to JSON serialize an integer that represents a game’s score.

We determine the score by summing the individual scores of five tries of the game using the numpy.sum method. We will cast the integers to dtype int32.

We will then store the score in a dictionary and pass the dictionary to the json.dumps() method to serialize the data.

import json
import numpy as np
score = np.sum([2, 4, 9, 12, 5], dtype=np.int32)
score_dict = {'score':score}
json_str = json.dumps(score_dict)
print(json_str)

Let’s run the code to see the result:

TypeError: Object of type int32 is not JSON serializable

The error occurs because the score object is of type int32 not int. Numpy methods return numpy integers, not ordinary Python integers. We can verify this by using the type() function.

print(type(score))
<class 'numpy.int32'>

Solution #1: Convert int32 to an integer using int()

The simplest way to solve this error is to pass the score variable to the int() function to convert it to a Python integer. We will add some print statements to demonstrate the conversion from int32 to int.

Let’s look at the updated code:

import json
import numpy as np

score = np.sum([2, 4, 9, 12, 5], dtype=np.int32)

print(type(score))

score = int(score)

print(type(score))

score_dict = {'score':score}

json_str = json.dumps(score_dict)

print(json_str)

Let’s run the code to get the result:

<class 'numpy.int32'>
<class 'int'>
{"score": 32}

We can see that we started with score as a numpy.int32 object, converted it to an int then serialized the dictionary containing the score to a JSON string.

Solution #2: Define a custom function for default kwarg

We can define a custom function that converts the int32 to an int.

We can then pass this function to json.dumps() as the default keyword argument.

The default value for the keyword argument default is None.

import json
import numpy as np

def serialize_int32(obj):

    if isinstance(obj, np.int32):

        return int(obj)

    raise TypeError ("Type %s is not serializable" % type(obj))

The custom function checks if the object is of type np.int32, converts it to an int and returns it.

Otherwise, the custom function raises a TypeError.

Let’s set the default keyword to our custom function and run the code:

import json
import numpy as np

score = np.sum([2, 4, 9, 12, 5], dtype=np.int32)

score_dict = {'score':score}

json_str = json.dumps(score_dict, default=serialize_int32)

print(json_str)
{"score": 32}

We successfully serialized the data in the file to a JSON string.

Solution #3: Define a JSONEncoder subclass for the cls kwarg

The third way we can solve this error is by building a custom JSONEncoder subclass. This subclass will override the default method to serialize additional types.

Similar to the custom function, the default method checks if the object is of type np.int32, converts it to an integer and returns it.

import json
import numpy as np

class int32_encoder(json.JSONEncoder):

    def default(self, obj):

        if isinstance(obj, np.int32):

            return int(obj)

        return json.JSONEncoder.default(self, obj)

We have to specify the custom JSONEncoder subclass with the cls keyword argument. Otherwise, JSONEncoder is used. Let’s look at the updated code:

import json
import numpy as np

score = np.sum([2, 4, 9, 12, 5], dtype=np.int32)

score_dict = {'score':score}

json_str = json.dumps(score_dict, cls=int32_encoder)

print(json_str)

Let’s run the code to get the result:

{"score": 32}

Below is the collection of objects that the JSONEncoder class supports and their JSON equivalent.

PythonJSON
dictobject
list, tuplearray
strstring
int, float, int- & float- derived Enumsnumber
Truetrue
Falsefalse
Nonenull
JSONEncoder Supported objects and types by default

What is the difference between Python Integer and numpy.int32?

Python 3 integers are flexible-sized, meaning they can grow to accommodate numbers of any size within memory constraints. NumPy integers are fixed in size, which means there is a maximum value they can hold. The number of bytes defines the maximum value in the integer, for example, int32 and int64.

int32 is a signed thirty-two bit integer value and has a min/max of -2147483648 to 2147483647

int64 is a signed, sixty-four bit integer and has a min/max of -9223372036854775808 to 9223372036854775807.

More bytes means that the integer can hold larger numbers.

Fixed-size integers facilitate fast calculations with less memory overhead than flexible-sized integers. Fixed-size integers can occupy consistently-sized adjacent memory blocks of the same types, which is how numpy arrays store data. The rapid calculations using numpy arrays would not be feasible with variable-sized Python integers because there is no way to fix the memory block size or the data type.

Numpy integers also have numpy.ndarray methods and attributes.

Summary

Congratulations on reading to the end of this tutorial.

For further reading on errors involving JSON serialization, go to the articles:

Go to the online courses page on Python to learn more about Python for data science and machine learning.