Attempting to serialize into JSON a dictionary that contains Numpy-specific types such as np.ndarray
, np.float64
, np.uint32
, etc. will generaly lead to error such as:
TypeError: Object of type ndarray is not JSON serializable
TypeError: Object of type float32 is not JSON serializable
To achieve a clean serialization, one must first cast all Numpy-type values into something more Python-friendly. This is done by extending the base JSONEncoder
:
import json
import numpy as np