NumPy array is not JSON serializable NumPy array is not JSON serializable json json

NumPy array is not JSON serializable


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

import numpy as npimport codecs, json a = np.arange(10).reshape(2,5) # a 2 by 5 arrayb = a.tolist() # nested lists with same data, indicesfile_path = "/path.json" ## your path variablejson.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)


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)


I found the best solution if you have nested numpy arrays in a dictionary:

import jsonimport numpy as npclass NumpyEncoder(json.JSONEncoder):    """ Special json encoder for numpy types """    def default(self, obj):        if isinstance(obj, np.integer):            return int(obj)        elif isinstance(obj, np.floating):            return float(obj)        elif isinstance(obj, np.ndarray):            return obj.tolist()        return json.JSONEncoder.default(self, obj)dumped = json.dumps(data, cls=NumpyEncoder)with open(path, 'w') as f:    json.dump(dumped, f)

Thanks to this guy.