Making numpy arrays JSON serializable Making numpy arrays JSON serializable python-3.x python-3.x

Making numpy arrays JSON serializable


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.