I have a program which handles data in the form of NumPy arrays, which needs to be stored in JSON form (to later pass it to another program to visualize the data). When I try the following:
my_array = np.array([3, 4, 5])
json.dumps(my_array)
I get an error message reading
TypeError: array([3, 4, 5]) is not JSON serializable
So it turns out that arrays are not serializable. I hoped to solve this by converting the arrays into ordinary lists, but if I try
my_array = np.array([3, 4, 5])
my_list = list(my_array)
json.dumps(my_list)
I just get an error reading
TypeError: 3 is not JSON serializable
(That 3
appears to be because '3' is the first element of the list)
Even stranger, this error persists when I try to reconstruct a list from scratch:
def plain_list(ls):
pl = []
for element in ls:
pl.append(element)
return pl
my_array = np.array([3, 4, 5])
my_list = plain_list(my_array)
json.dumps(my_list)
still returns
TypeError: 3 is not JSON serializable
I have two questions:
- How is it that even that last one doesn't work? Shouldn't it have 'forgotten' everything about having ever been a NumPy array? Are integers in NumPy arrays not really the same objects as ordinary integers, and do they carry some special invisible property with them that persists even outside of the array?
- How can I effectively store arrays in JSON form (no matter how convoluted the solution may need to be)?