Skip to content
Advertisement

How to convert nested numpy arrays in a dictionary to JSON?

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?

Advertisement

Answer

You can make custom json.JSONEncoder. For example:

import json


class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        return json.JSONEncoder.default(self, obj)


data = {"data": [{"obj": np.array([1, 2, 3])}]}

print(json.dumps(data, cls=MyEncoder, indent=4))

Prints:

{
    "data": [
        {
            "obj": [
                1,
                2,
                3
            ]
        }
    ]
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement