Make NDArray JSON serializable?


#1

For the following code:

from mxnet import nd
import json

a = nd.array([[0,1,2],[3,4,5],[6,7,8]])
json.dumps({'a':a})

It gives the following error:

TypeError: Object of type 'NDArray' is not JSON serializable

This is not surprising; numpy.array gives the same error. Then, what is the most efficient way of serializing NDArray into JSON? Thanks!


#2

Hi @jdchoi77,

JSON doesn’t seem a particularity efficient serialization format for this type of data in the first place, but if you’ve got a strict requirement for this you could create your own encoder for this. Converting data from NDArray to JSON, could be obtained using asnumpy().tolist(), but this won’t be hugely performant.

import mxnet as mx
import json

# random integers
data = mx.nd.random.poisson(lam=1, shape=(3,3))

class NDArrayEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, mx.nd.NDArray):
            return obj.asnumpy().tolist()
        return json.JSONEncoder.default(self, obj)
    
json_str = json.dumps({'test': data}, cls=NDArrayEncoder, indent=4)
print(json_str)
{
    "test": [
        [
            0.0,
            0.0,
            3.0
        ],
        [
            1.0,
            2.0,
            1.0
        ],
        [
            1.0,
            0.0,
            0.0
        ]
    ]
}

As an alternative, you might want to look at mxnet.ndarray.save which will be more performant for this I believe. And you could work out a similar solution which sends the binary data (via base64) inside the JSON.