For the following code:
import json
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
normalString = str(arr)
print(normalString)
jsonString = json.dumps(arr)
print(jsonString)
Converting this to a string and then printing it works just fine; as expected, it prints out [1, 2, 3, 4, 5]
. However, when I try to serialize it to JSON, I get TypeError: Object of type ndarray is not JSON serializable
for the line where I'm calling json.dumps
. Why isn't a Numpy array "directly" serializable to JSON?
I'm aware of the fact that this can be circumvented by converting the Numpy array to a list before trying to serialize it. There are numerous tutorials and Q&As that explain that fact, such as the previously-linked Q&A as well as this one. However, none of the Q&As I've found so far explain why this is the case. The closest I've found so far is a comment on the latter post explaining that
...
numpy.ndarray
is not a type that json knows how to handle. You'll either need to write your own serializer, or (more simply) just pass list(your_array) to whatever is writing the json.
However, this comment doesn't explain exactly why the json module doesn't know how to handle this. All of the answers to the question explain how to correct the problem, not why it occurs in the first place.
Can someone explain why the json
model doesn't know how to handle this but the str
function does? Again, I'm not asking how to fix this, as the linked Q&As already explain that.