Python - TypeError: Object of type 'int64' is not JSON serializable
Full error message
I have a data frame that stores store name and daily sales count. I am trying to insert this to Salesforce using the Python script below.
However, I get the following error:
TypeError: Object of type 'int64' is not JSON serializable
Below, there is the view of the data frame.
Storename,Count
Store A,10
Store B,12
Store C,5
I use the following code to insert it to Salesforce.
update_list = []
for i in range(len(store)):
update_data = {
'name': store['entity_name'].iloc[i],
'count__c': store['count'].iloc[i]
}
update_list.append(update_data)
sf_data_cursor = sf_datapull.salesforce_login()
sf_data_cursor.bulk.Account.update(update_list)
I get the error when the last line above gets executed.
How do I fix this?Solutionsource: stackoverflow \u2197
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)
API access
Get this solution programmatically \u2014 free, no authentication.
curl https://depscope.dev/api/error/bb33c279ac4b84d3ce3834177cecaf6d58d6eec6e491b3583e68ca57e75e0987hash \u00b7 bb33c279ac4b84d3ce3834177cecaf6d58d6eec6e491b3583e68ca57e75e0987