Dumping 2D Python array with Json

json, python

Solution

what worked for me - since having larger 1024x1002 arrays of float64 - was conversion to base64.

def Base64Encode(ndarray):
    return json.dumps([str(ndarray.dtype),base64.b64encode(ndarray),ndarray.shape])
def Base64Decode(jsonDump):
    loaded = json.loads(jsonDump)
    dtype = np.dtype(loaded[0])
    arr = np.frombuffer(base64.decodestring(loaded[1]),dtype)
    if len(loaded) > 2:
        return arr.reshape(loaded[2])
    return arr

''' just to compare '''
def SimpleEncode(ndarray):
    return json.dumps(ndarray.tolist())
def SimpleDecode(jsonDump):
    return np.array(json.loads(jsonDump))

ipython %timeit result points very clearly to base64:

arr = np.random.random_sample((1000, 1000))

print 'Simple Convert'
%timeit SimpleDecode(SimpleEncode(arr))
print 'Base64 Encoding'
%timeit Base64Decode(Base64Encode(arr))

result:

Simple Convert
1 loops, best of 3: 1.42 s per loop
Base64 Encoding
10 loops, best of 3: 171 ms per loop

Problem

I have a `numpy` array that I would like to dump with Json. The array looks like this: ``` array([['foo', 'bar', 'something', ... 'more'], ['0.4', '0.7', '0.83', ... '0.3', '0.62', '0.51']] ``` and I would like to dump it on a string with Json as follows: ``` foo: 0.4 bar: 0.7 something: 0.51 ... ``` I have tried with: ``` import jason my_string = json.dumps(my_array) ``` but it complains with: ``` "not JSON serializable" ``` Any thoughts on how to dump this on a string with Json? Update: Please not that I care about ordering, lines should be printed in the following order: ``` array[0,0] : array[0,1] array[1,0] : array[1,1] array[2,0] : array[2,1] # etc ... ```

Original source