TypeError: Object of type ndarray is not JSON serializable

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
5 min

banner

# Table of Contents

  1. TypeError: Object of type ndarray is not JSON serializable
  2. TypeError: Object of type DataFrame is not JSON serializable

# TypeError: Object of type ndarray is not JSON serializable

The Python "TypeError: Object of type ndarray is not JSON serializable" occurs when we try to convert a NumPy ndarray object to a JSON string.

To solve the error, use the tolist() method on the array to convert it to a Python list before serializing it to JSON.

Here is an example of how the error occurs.

main.py
import json import numpy as np arr = np.array([1, 2, 3, 4]) # ⛔️ TypeError: Object of type ndarray is not JSON serializable json_str = json.dumps({'nums': arr})

object of type ndarray is not json serializable

We tried passing a NumPy ndarray object to the json.dumps() method but the method doesn't handle NumPy arrays by default.

# Convert the array to a Python list to solve the error

To solve the error, use the tolist() method on the array to convert it to a Python list.

main.py
import json import numpy as np arr = np.array([1, 2, 3, 4]) # ✅ used tolist() json_str = json.dumps({'nums': arr.tolist()}) print(json_str) # 👉️ {"nums": [1, 2, 3, 4]} print(type(json_str)) # 👉️ <class 'str'>

convert array to python list to solve the error

We can use a native Python list instead of a NumPy ndarray when serializing to JSON.

The json.dumps() method converts a Python object to a JSON formatted string.

If you need to convert the JSON string back to a NumPy array, use the json.loads() method.

main.py
import json import numpy as np arr = np.array([1, 2, 3, 4]) # ✅ used tolist() json_str = json.dumps({'nums': arr.tolist()}) my_dict = json.loads(json_str) new_arr = np.array(my_dict['nums']) print(new_arr) # 👉️ [1 2 3 4]

use json loads to convert json string to numpy array

The json.loads() method parses a JSON string into a native Python object.

We accessed the Python list and used the np.array method to convert it to a NumPy array.

# Extend from the JSONEncoder class to solve the error

Alternatively, you can extend from the JSONEncoder class and handle the conversions in a default method.

main.py
import json import numpy as np class NpEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.integer): return int(obj) if isinstance(obj, np.floating): return float(obj) if isinstance(obj, np.ndarray): return obj.tolist() return json.JSONEncoder.default(self, obj) arr = np.array([1, 2, 3, 4]) json_str = json.dumps({'nums': arr}, cls=NpEncoder) print(json_str) # 👉️ {"nums": [1, 2, 3, 4]} print(type(json_str)) # 👉️ <class 'str'>

extend the json encoder class to solve the error

We extended from the JSONEncoder class.

The JSONEncoder class supports the following objects and types by default.

PythonJSON
dictobject
list, tuplearray
strstring
int, float, int and float derived Enumsnumber
Truetrue
Falsefalse
Nonenull

Notice that the JSONEncoder class doesn't support NumPy ndarray to JSON conversion by default.

We can handle this by extending from the class and implementing a default() method that returns a serializable object.

main.py
import json import numpy as np class NpEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.integer): return int(obj) if isinstance(obj, np.floating): return float(obj) if isinstance(obj, np.ndarray): return obj.tolist() return json.JSONEncoder.default(self, obj)

If the passed-in object is an instance of np.integer, we convert the object to a Python int() and return the result.

If the passed-in object is an instance of np.floating, we convert it to a Python float and return the result.

If the object is an instance of np.ndarray, we convert it to a Python list and return the result.

In all other cases, we let the base class's default method do the serialization.

To use a custom JSONEncoder, specify it with the cls keyword argument in your call to the json.dumps() method.

main.py
import json import numpy as np class NpEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.integer): return int(obj) if isinstance(obj, np.floating): return float(obj) if isinstance(obj, np.ndarray): return obj.tolist() return json.JSONEncoder.default(self, obj) arr = np.array([1, 2, 3, 4]) # ✅ provide cls keyword argument json_str = json.dumps({'nums': arr}, cls=NpEncoder) print(json_str) # 👉️ {"nums": [1, 2, 3, 4]} print(type(json_str)) # 👉️ <class 'str'>

If you don't provide the cls kwarg, the default JSONEncoder is used.

# Using the default keyword argument to solve the error

You can also use the default keyword argument in the call to the json.dumps() method.

main.py
import json import numpy as np arr = np.array([1, 2, 3, 4]) def json_serializer(obj): if isinstance(obj, np.ndarray): return obj.tolist() return obj json_str = json.dumps({'nums': arr}, default=json_serializer) print(json_str) # 👉️ {"nums": [1, 2, 3, 4]}

using default keyword argument to solve the error

The json.dumps() method converts a Python object to a JSON formatted string.

The default keyword argument can be set to a function that gets called for objects that can't otherwise be serialized.

We simply convert the ndarray object to a list by using the tolist() method.

The json_serializer function takes an object as a parameter and checks if the object is a ndarray.

If the condition is met, we use the tolist() method to convert the array to a list, otherwise, the value is returned as is.

# Using pandas to solve the error

If you use the pandas module, you can also use the to_json method to solve the error.

main.py
import numpy as np import pandas as pd arr = np.array([1, 2, 3, 4]) json_str = pd.Series(arr).to_json(orient='values') print(json_str) # 👉️ [1,2,3,4]

The Series.to_json method converts the object to a JSON string.

The orient argument determines the expected JSON string format.

The values option is used to only use the array's values in the JSON string.

# TypeError: Object of type DataFrame is not JSON serializable

The Python "TypeError: Object of type DataFrame is not JSON serializable" occurs when we try to serialize a DataFrame object to JSON using the json.dumps method.

To solve the error, use the to_json() method instead, e.g. df.to_json().

Here is an example of how the error occurs

main.py
import json import pandas as pd df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) # ⛔️ TypeError: Object of type DataFrame is not JSON serializable print(json.dumps(df))

We tried passing a DataFrame object to the json.dumps() method but the method doesn't handle DataFrame objects by default.

# Use the to_json() method to solve the error

To solve the error, use the to_json() method on the DataFrame object.

main.py
import pandas as pd df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) # 👇️ '{"Name":{"0":"Alice","1":"Bob","2":"Carl"},"Age":{"0":29,"1":30,"2":31}}' print(df.to_json()) # 👇️ <class 'str'> print(type(df.to_json()))

The to_json() method converts the DataFrame object to a JSON string.

# Convert the DataFrame to a dict to solve the error

Alternatively, you can try to convert the DataFrame to a dict object before serializing to JSON.

main.py
import json import pandas as pd df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) # 👇️ '{"Name": {"0": "Alice", "1": "Bob", "2": "Carl"}, "Age": {"0": 29, "1": 30, "2": 31}}' print(json.dumps(df.to_dict())) # 👇️ <class 'str'> print(type(json.dumps(df.to_dict())))

The to_dict method converts the DataFrame to a dictionary, which we can then serialize to JSON.

The JSONEncoder class supports the following objects and types by default.

PythonJSON
dictobject
list, tuplearray
strstring
int, float, int and float derived Enumsnumber
Truetrue
Falsefalse
Nonenull
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.