"TypeError: (Integer) is not JSON serializable" when serializing JSON in Python?
Solution 1
I found my problem. The issue was that my integers were actually type numpy.int64
.
Solution 2
It seems like there may be a issue to dump numpy.int64 into json string in Python 3 and the python team already have a conversation about it. More details can be found here.
There is a workaround provided by Serhiy Storchaka. It works very well so I paste it here:
def convert(o):
if isinstance(o, numpy.int64): return int(o)
raise TypeError
json.dumps({'value': numpy.int64(42)}, default=convert)
Solution 3
as @JAC pointed out in the comments of the highest rated answer, the generic solution (for all numpy types) can be found in the thread Converting numpy dtypes to native python types.
Nevertheless, I´ll add my version of the solution below, as my in my case I needed a generic solution that combines these answers and with the answers of the other thread. This should work with almost all numpy types.
def convert(o):
if isinstance(o, np.generic): return o.item()
raise TypeError
json.dumps({'value': numpy.int64(42)}, default=convert)
Solution 4
Just convert numbers from int64
(from numpy) to int
.
For example, if variable x
is a int64:
int(x)
If is array of int64:
map(int, x)
Solution 5
You have Numpy Data Type, Just change to normal int()
or float()
data type. it will work fine.
Related videos on Youtube
user1329894
Updated on July 25, 2022Comments
-
user1329894 almost 2 years
I am trying to send a simple dictionary to a json file from python, but I keep getting the "TypeError: 1425 is not JSON serializable" message.
import json alerts = {'upper':[1425],'lower':[576],'level':[2],'datetime':['2012-08-08 15:30']} afile = open('test.json','w') afile.write(json.dumps(alerts,encoding='UTF-8')) afile.close()
If I add the default argument, then it writes, but the integer values are written to the json file as strings, which is undesirable.
afile.write(json.dumps(alerts,encoding='UTF-8',default=str))
-
Shiplu Mokaddim almost 12 years
-
Admin almost 12 yearsThis doesn't appear to "duplicate" that question ..
-
user1329894 almost 12 yearsI found my problem. The issue was that my integers were actually type numpy.int64.
-
Admin almost 12 years@user1329894 Post as a solution/explanation and self-close ..
-
Russell Borogove almost 12 years-0 for writing a minimal repro that doesn't actually reproduce the bug.
-
-
JAC over 10 yearsI had to deal with this issue too, and your answer pointed me in the right direction. I just wanted to add a link to another question that can help in actually solving the problem.
-
Franck Dernoncourt almost 9 yearsThat'd be nice if the JSON unserializable error message could display the type of the object...
-
Owen almost 8 yearsHere is a tidy solution that uses a custom serializer.
-
boson over 7 years@Owen This is a great find
-
Abhishek Lodha almost 7 yearsIt works perfectly on ubuntu but not on windows. Please help.
-
Jerdak over 6 yearsGreat catch. Happens with AWS Lambda (Python 3.6) serialization as well. (forums.aws.amazon.com/message.jspa?messageID=689708)
-
BallpointBen about 6 yearsThat's the problem, but what's the solution?
-
zelcon about 6 yearsx.astype(int) or int(x)
-
LMSharma over 5 yearsx.astype(int) or int(x) not working in python3.5. this is what i am doing. data['id'] = data['id'].astype(int)
-
Raveen Beemsingh over 5 yearsthe serializer solution worked for me in my flask project
-
Pranzell about 5 yearsA wonderful workaround provided by Serhiy. Please check his approach. And to add, just: json.dumps(yourObject, default=default); like here.
-
Gaurav Bishnoi over 4 yearsFor me, just int(x) is working, not astype(x). Strange.
-
jtlz2 about 4 yearsNice answer indeed
-
user319436 about 4 yearsJust noticed eval(str()) is very slow so use with caution. @shiva's answer is much better: json.dumps(alerts, default=myconverter)