9

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:

  1. 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?
  2. How can I effectively store arrays in JSON form (no matter how convoluted the solution may need to be)?
Drubbels
  • 327
  • 2
  • 4
  • 11

1 Answers1

15

That 3 is a NumPy integer that displays like a regular Python int, but isn't one. Use tolist to get a list of ordinary Python ints:

json.dumps(my_array.tolist())

This will also convert multidimensional arrays to nested lists, so you don't have to deal with a 3-layer list comprehension for a 3-dimensional array.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • 1
    In addition to user2357112's correct answer about how to specifically get your code working, consider extending `json` to be able to encode your array as specified [here](https://stackoverflow.com/a/24375113/3171657). – Turn Jan 17 '18 at 21:19