Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NumPy array is not JSON serializable

After creating a NumPy array, and saving it as a Django context variable, I receive the following error when loading the webpage:

array([   0,  239,  479,  717,  952, 1192, 1432, 1667], dtype=int64) is not JSON serializable 

What does this mean?

like image 446
Karnivaurus Avatar asked Oct 30 '14 06:10

Karnivaurus


People also ask

How do I serialize a NumPy array to JSON?

Use the cls kwarg of the json. dump() and json. dumps() method to call our custom JSON Encoder, which will convert NumPy array into JSON formatted data. To serialize Numpy array into JSON we need to convert it into a list structure using a tolist() function.

Is NumPy array serializable?

As far as I know you can not simply serialize a numpy array with any data type and any dimension...but you can store its data type, dimension and information in a list representation and then serialize it using JSON.

Is not JSON serializable?

The Python "TypeError: Object of type function is not JSON serializable" occurs when we try to serialize a function to JSON. To solve the error, make sure to call the function and serialize the object that the function returns.


2 Answers

I regularly "jsonify" np.arrays. Try using the ".tolist()" method on the arrays first, like this:

import numpy as np import codecs, json   a = np.arange(10).reshape(2,5) # a 2 by 5 array b = a.tolist() # nested lists with same data, indices file_path = "/path.json" ## your path variable json.dump(b, codecs.open(file_path, 'w', encoding='utf-8'),            separators=(',', ':'),            sort_keys=True,            indent=4) ### this saves the array in .json format 

In order to "unjsonify" the array use:

obj_text = codecs.open(file_path, 'r', encoding='utf-8').read() b_new = json.loads(obj_text) a_new = np.array(b_new) 
like image 76
travelingbones Avatar answered Sep 19 '22 08:09

travelingbones


Store as JSON a numpy.ndarray or any nested-list composition.

class NumpyEncoder(json.JSONEncoder):     def default(self, obj):         if isinstance(obj, np.ndarray):             return obj.tolist()         return json.JSONEncoder.default(self, obj)  a = np.array([[1, 2, 3], [4, 5, 6]]) print(a.shape) json_dump = json.dumps({'a': a, 'aa': [2, (2, 3, 4), a], 'bb': [2]},                         cls=NumpyEncoder) print(json_dump) 

Will output:

(2, 3) {"a": [[1, 2, 3], [4, 5, 6]], "aa": [2, [2, 3, 4], [[1, 2, 3], [4, 5, 6]]], "bb": [2]} 

To restore from JSON:

json_load = json.loads(json_dump) a_restored = np.asarray(json_load["a"]) print(a_restored) print(a_restored.shape) 

Will output:

[[1 2 3]  [4 5 6]] (2, 3) 
like image 44
karlB Avatar answered Sep 21 '22 08:09

karlB