"TypeError: (Integer) is not JSON serializable" when serializing JSON in Python?

151,263

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.

Share:
151,263

Related videos on Youtube

user1329894
Author by

user1329894

Updated on July 25, 2022

Comments

  • user1329894
    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
      Shiplu Mokaddim almost 12 years
    • Admin
      Admin almost 12 years
      This doesn't appear to "duplicate" that question ..
    • user1329894
      user1329894 almost 12 years
      I found my problem. The issue was that my integers were actually type numpy.int64.
    • Admin
      Admin almost 12 years
      @user1329894 Post as a solution/explanation and self-close ..
    • Russell Borogove
      Russell Borogove almost 12 years
      -0 for writing a minimal repro that doesn't actually reproduce the bug.
  • JAC
    JAC over 10 years
    I 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
    Franck Dernoncourt almost 9 years
    That'd be nice if the JSON unserializable error message could display the type of the object...
  • Owen
    Owen almost 8 years
    Here is a tidy solution that uses a custom serializer.
  • boson
    boson over 7 years
    @Owen This is a great find
  • Abhishek Lodha
    Abhishek Lodha almost 7 years
    It works perfectly on ubuntu but not on windows. Please help.
  • Jerdak
    Jerdak over 6 years
    Great catch. Happens with AWS Lambda (Python 3.6) serialization as well. (forums.aws.amazon.com/message.jspa?messageID=689708)
  • BallpointBen
    BallpointBen about 6 years
    That's the problem, but what's the solution?
  • zelcon
    zelcon about 6 years
    x.astype(int) or int(x)
  • LMSharma
    LMSharma over 5 years
    x.astype(int) or int(x) not working in python3.5. this is what i am doing. data['id'] = data['id'].astype(int)
  • Raveen Beemsingh
    Raveen Beemsingh over 5 years
    the serializer solution worked for me in my flask project
  • Pranzell
    Pranzell about 5 years
    A wonderful workaround provided by Serhiy. Please check his approach. And to add, just: json.dumps(yourObject, default=default); like here.
  • Gaurav Bishnoi
    Gaurav Bishnoi over 4 years
    For me, just int(x) is working, not astype(x). Strange.
  • jtlz2
    jtlz2 about 4 years
    Nice answer indeed
  • user319436
    user319436 about 4 years
    Just noticed eval(str()) is very slow so use with caution. @shiva's answer is much better: json.dumps(alerts, default=myconverter)