. Advertisement .
..3..
. Advertisement .
..4..
Hi everyone, I’m learning about python. While working, I try load the webpage after creating a NumPy array, and saving it as a Django context variable. As a result, I get the message:
array([ 0, 239, 479, 717, 952, 1192, 1432, 1667], dtype=int64) is not JSON serializable
What can I do about the “typeerror object of type ndarray is not json serializable” issue? Is there a better approach?
The cause:
The error happens because you are attempting to change a NumPy ndarray object into a JSON string but the default function which is called for objects can not be serialized, so it raises an error.
Solution:
You can solve this problem by applying “.tolist()” method for the first arrays as the below:
Next, let’s use these following commands to “unjsonify” the array:
Save as JSON a numpy.ndarray and any other nested-list component.
What will output look like?
To restore using JSON:
What will output look like?