array to json (TypeError: Object of type ndarray is not JSON serializable)
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) 출처 : https://stackoverflow.com/questions/26646362/numpy-array-is-not-json-serializable
2022. 4. 24.