Mohammed Gadi
Mohammed Gadi

Reputation: 391

How to convert dictionary with numpy array to json and back?

I am learning python development and I am new to the python world, below is my dictionary with values as NumPy array and I want to convert it to JSON, and convert it back to the dictionary with NumPy array from JSON. Actually, I am trying to convert it using json.dumps() but it is giving me an error that says: the object of type ndarray is not JSON serializable

{
  'chicken': array([5. , 4. , 3. , 2. , 1. , 0.5, 0. ]),
  'banana': array([4. , 3. , 2. , 1. , 0.5, 0. ]),
  'carrots': array([5. , 4. , 3. , 2. , 1. , 0.5, 0. ]),
  'turkey': array([5. , 4. , 3. , 2. , 1. , 0.5, 0. ]),
  'rice': array([3. , 2. , 1. , 0.5, 0. ]),
  'whey': array([5. , 4. , 3. , 2. , 1. , 0.5, 0. ]),
  'peanut': array([5. , 4. , 3. , 2. , 1. , 0.5, 0. ]),
  'Yellow Cake (with Vanilla Frosting)': array([5. , 4. , 3. , 2. , 1. , 0.5, 0. ])
}

I am doing this because I want to pass data from one AWS Lambda function to another AWS Lambda function. Any help will be appreciated, Thanks.

Upvotes: 1

Views: 6068

Answers (2)

FangQ
FangQ

Reputation: 1544

Please see my solution posted here: https://stackoverflow.com/a/70884973/4271392

it uses a lightweight python module jdata to encode numpy arrays and save binary information, and converts from json back to numpy as well.

Upvotes: 0

mujjiga
mujjiga

Reputation: 16896

numpy arrays cannot be converted into json directly; instead use list.

# Test data
d = {
  'chicken': np.random.randn(5),
  'banana': np.random.randn(5),
  'carrots': np.random.randn(5)
}

# To json
j = json.dumps({k: v.tolist() for k, v in d.items()})

# Back to dict
a = {k: np.array(v) for k, v in json.loads(j).items()}

print (a)
print (d)

Output:

{'banana': array([-0.9936452 ,  0.21594978, -0.24991611,  0.99210387, -0.22347124]),
 'carrots': array([-0.7981783 , -1.47840335, -0.00831611,  0.58928124, -0.33779016]),
 'chicken': array([-0.03591249, -0.75118824,  0.58297762,  0.5260574 ,  0.6391851 ])}

{'banana': array([-0.9936452 ,  0.21594978, -0.24991611,  0.99210387, -0.22347124]),
 'carrots': array([-0.7981783 , -1.47840335, -0.00831611,  0.58928124, -0.33779016]),
 'chicken': array([-0.03591249, -0.75118824,  0.58297762,  0.5260574 ,  0.6391851 ])}

Upvotes: 6

Related Questions