I'm parsing nested dictionaries, which have different degrees of how nested they are (dictionaries within dictionaries within dictionaries, etc.) I do not know beforehand to what degree the dictionaries are nested.
The problem is, certain dictionary values are numpy.ndarrays. When I try to write the dictionary my_dictionary to JSON with
with open(my_dictionary, 'w') as f:
json.dump(my_dictionary, f, indent=4)
I will get the following error:
TypeError: Object of type ndarray is not JSON serializable
Naturally, one way to overcome this would be to simply convert all numpy.ndarray values into a list with .tolist().
However, given I do not know how nested the dictionaries are, how could I algorithmically check all values of any nested dictionary and convert ndarray to list?
Or is there another way to overcome this error?