Python - TypeError: Object Of Type 'Int64' Is Not JSON Serializable

875    Asked by ankur_3579 in Python , Asked on Nov 16, 2022

 have a Dataframe that stores Store name and daily sales count. I am trying to insert this to Salesforce using the below Python script. I, however, get an error.

TypeError: Object of type 'int64' is not JSON serializable
Given below is the view of the dataframe:
Storename,Count
Store A,10
Store B, 12
Store C, 5
I use the below 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)
Get the error the last line above gets executed. Could anyone assist in fixing this.
Answered by Anisha Dalal

To solve the error - object of type int64 is not json serializable -


My solution

numpyencoder and its repository.
from numpyencoder import NumpyEncoder
numpy_data = np.array([0, 1, 2, 3])
with open(json_file, 'w') as file:
    json.dump(numpy_data, file, indent=4, sort_keys=True,
              separators=(', ', ': '), ensure_ascii=False,
              cls=NumpyEncoder)
The breakdown
If you dig into hmallen's code in the numpyencoder/numpyencoder.py file you'll see that it's very similar to @Jie Yang's answer:
Products
2022 Developer Survey is open! Take a survey.
Python - TypeError: Object of type 'int64' is not JSON serializable
Asked 3 years, 11 months ago
Modified 6 months ago
Viewed 180k times
155
15
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?
python
numpy
Share
Improve this question
Follow
edited Jul 6, 2021 at 22:55
user avatar
Russia Must Remove Putin♦
336k8383 gold badges390390 silver badges324324 bronze badges
asked Jun 18, 2018 at 19:39
user avatar
dark horse
2,49377 gold badges1717 silver badges3434 bronze badges
That call to range is suspicious. You are taking len(store) and wrapping that in a tuple, and then calling range on the tuple. If you remove one set of parentheses, does it fix the code? That is, try this: for i in range(len(store)):. –
Tim Johns
 Jun 18, 2018 at 20:00
@TimJohns A pair of parentheses around a number does not make it a tuple. (34) is still a number 34. But (34,) is a tuple. –
DYZ
 Jun 18, 2018 at 20:04
@DyZ Good point, I didn't realise that parentheses with a single argument are treated differently than if there are multiple arguments. –
Tim Johns
 Jun 18, 2018 at 20:13
2
@TimJohns The parents are irrelevant. a=34, is also a tuple –
Sebastian Wozny
 Feb 20, 2020 at 15:27
There is an open bug report about this issue: bugs.python.org/issue24313 –
Mihai Capotă
 Jul 22, 2020 at 20:59
Add a comment
10 Answers
Sorted by:
Highest score (default)
159
json does not recognize NumPy data types. Convert the number to a Python int before serialise the object:
'count__c': int(store['count'].iloc[i])
Share
Improve this answer
Follow
answered Jun 18, 2018 at 20:07
user avatar
DYZ
51.2k99 gold badges5959 silver badges8686 bronze badges
Thanks a lot buddy! –
codingbruh
 May 26, 2021 at 10:04
Add a comment
142
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)
Share
Improve this answer
Follow
edited Jul 31, 2021 at 11:24
user avatar
Tommy
11.2k1111 gold badges5757 silver badges9191 bronze badges
answered Sep 12, 2019 at 22:29
user avatar
Jie Yang
1,54711 gold badge77 silver badges33 bronze badges
2
@IvanCamilitoRamirezVerdes That's exactly what I was looking for so it was quite helpful, for me. I like the readability of throwing in an encoding class to the json.dumps function as well. –
Jason R Stevens CFA
 May 20, 2020 at 1:54
did not work for me for some reason... Does this support recursive calls? –
adir abargil
 Dec 2, 2020 at 14:43
2
I also needed to add elif isinstance(obj, np.bool_): return bool(obj) –
James McKeown
 Feb 8, 2021 at 19:32
2
I've edited this answer to remove the unnecessary "elifs' ' since there are returns. –
Tommy
 Jul 31, 2021 at 11:28
For most cases, this is quite an overkill. The solution by Tharindu Sathischandra is more straightforward. –
Gnnr
 Mar 11 at 7:47
Add a comment
33
I'll throw in my answer to the ring as a bit more stable version of @Jie Yang's excellent solution.
My solution
numpyencoder and its repository.
from numpyencoder import NumpyEncoder
numpy_data = np.array([0, 1, 2, 3])
with open(json_file, 'w') as file:
    json.dump(numpy_data, file, indent=4, sort_keys=True,
              separators=(', ', ': '), ensure_ascii=False,
              cls=NumpyEncoder)
The breakdown
If you dig into hmallen's code in the numpyencoder/numpyencoder.py file you'll see that it's very similar to @Jie Yang's answer:
class NumpyEncoder(json.JSONEncoder):
    """ Custom encoder for numpy data types """
    def default(self, obj):
        if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
                            np.int16, np.int32, np.int64, np.uint8,
                            np.uint16, np.uint32, np.uint64)):
            return int(obj)
        elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)):
            return float(obj)
        elif isinstance(obj, (np.complex_, np.complex64, np.complex128)):
            return {'real': obj.real, 'imag': obj.imag}
        elif isinstance(obj, (np.ndarray,)):
            return obj.tolist()
elif isinstance(obj, (np.bool_)):
            return bool(obj)
        elif isinstance(obj, (np.void)):
            return None
        return json.JSONEncoder.default(self, obj)


Your Answer

Interviews

Parent Categories