Python - TypeError: Object of type 'int64' is not JSON serializable
Solution 1:
json
does not recognize NumPy data types. Convert the number to a Python int
before serializing the object:
'count__c': int(store['count'].iloc[i])
Solution 2:
You can define your own encoder to solve this problem.
import json
import numpy as np
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
if isinstance(obj, np.floating):
return float(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
return super(NpEncoder, self).default(obj)
# Your codes ....
json.dumps(data, cls=NpEncoder)