close

[Solved] “TypeError: (Integer) is not JSON serializable” when serializing JSON in Python?

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error “TypeError: (Integer) is not JSON serializable” when serializing JSON in Python? in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How “TypeError: (Integer) is not JSON serializable” when serializing JSON in Python? Error Occurs?

Today I get the following error “TypeError: (Integer) is not JSON serializable” when serializing JSON in Python? in python.

How To Solve “TypeError: (Integer) is not JSON serializable” when serializing JSON in Python? Error ?

  1. How To Solve “TypeError: (Integer) is not JSON serializable” when serializing JSON in Python? Error ?

    To Solve “TypeError: (Integer) is not JSON serializable” when serializing JSON in Python? Error I found my problem. The issue was that my integers were actually type numpy.int64.

  2. “TypeError: (Integer) is not JSON serializable” when serializing JSON in Python?

    To Solve “TypeError: (Integer) is not JSON serializable” when serializing JSON in Python? Error I found my problem. The issue was that my integers were actually type numpy.int64.

Solution 1

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 2

I found my problem. The issue was that my integers were actually type numpy.int64.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read