Explain Codes LogoExplain Codes Logo

Numpy array is not JSON serializable

python
numpy
json-serialization
data-serialization
Anton ShumikhinbyAnton Shumikhin·Oct 12, 2024
TLDR

Easily convert a NumPy array to JSON by transforming it into a list with tolist() and serializing with json.dumps():

import json import numpy as np np_array = np.array([1, 2, 3]) # Convert and serialize like a charm! json_data = json.dumps(np_array.tolist())

Custom encoding for complex NumPy types

Some NumPy types don't play nice with JSON. Fear not! Craft a custom encoder to handle these troublesome types.

import json import numpy as np # Custom encoder at your service class NumpyEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.ndarray): return obj.tolist() return json.JSONEncoder.default(self, obj) np_array = np.array([1, 2, 3]) # Let the custom encoder do its magic json_data = json.dumps(np_array, cls=NumpyEncoder)

The custom encoder ensures your JSON stays friendly and consistent.

How to get your data back

Rescue your data from JSON format back to a NumPy array using good old np.array():

import json import numpy as np json_data = '[1, 2, 3]' list_data = json.loads(json_data) # Welcome back to the cool kids club, data! np_array = np.array(list_data)

Your data is now back as a high-performance NumPy array. High fives all round!

Storing data like a boss

For big data, file storage methods are key. Use codecs.open() for the perfect save:

import codecs import json import numpy as np np_array = np.array([1, 2, 3]) # codecs - because saving should be sassy! with codecs.open('datafile.json', 'w', encoding='utf-8') as f: json.dump(np_array.tolist(), f)

Your data is now safe and sound. Cheers to neuropreservation!

Manage multi-dimensional data elegantly

For those brain-melting multi-dimensional arrays, use Pandas and make your life simple:

import pandas as pd import numpy as np df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]}) json_data = df.to_json(orient='values') # Magic spell to bring our data back as a NumPy array np_array = np.array(json.loads(json_data))

Even the most complex datasets are no match for Pandas!

Conquering the beasts - numpy-specific types

Taming specialist NumPy data types for JSON serialization is as easy as pie:

import json import numpy as np # Tame the beast! def numpy_serializer(obj): if isinstance(obj, np.generic): return obj.item() raise TypeError("Type not serializable") np_array = np.array([1, 2, 3], dtype=np.int64) # JSON - meet your new friend, np.int64 json_data = json.dumps(np_array.tolist(), default=numpy_serializer)

With custom serialization, every NumPy type is your friend!