0

I have used a dataframe with country and service data to count the recurrence of service grouped by country and generated the dictionary d. I have been trying to convert the dict into json since dict type is not callable. But it is giving the following error. What can be done to change the int64 into string?

TypeError: Object of type 'int64' is not JSON serializable

I have tried :

d = {'India': {'A': 1, 'C': 2}, 'Malaysia': {'B': 1, 'A': 1, 'D': 1}, 'Croatia': {'C': 1}}
y = json.dumps(d)
return y
mtrw
  • 34,200
  • 7
  • 63
  • 71
prj
  • 61
  • 6
  • I copy&pasted your code example to my python console and it worked (except return because it isn't in a method). – ex4 Mar 02 '21 at 15:43
  • The code you posted [doesn't produce this error](https://tio.run/##K6gsycjPM/7/PzO3IL@oRCGrOD@PK0XBVqFa3TMvJTNR3QrIcgSShjoK6s5A2qgWyPBNzEmsLIbKOkFlYapcQDRIlXNRfmIJVJEzWLCWi6sSaDbIEr2U0tyCYo0Uzf//AQ "Python 3 – Try It Online"). Please provide a [mcve]. – ForceBru Mar 02 '21 at 15:43
  • Try it using a method. Because the use case is, taking data from excel sheet, converting into dataframe, dataframe into the dictionary, that into json . This is done in Flask framework. API is unable to call json with int64 value in it – prj Mar 03 '21 at 04:38

2 Answers2

0

Did you import json ? because following is working just fine

import json
d = {'India': {'A': 1, 'C': 2}, 'Malaysia': {'B': 1, 'A': 1, 'D': 1},               
'Croatia': {'C': 1}}

y = json.dumps(d)
print(type(y))
Machine
  • 103
  • 7
  • yes i did, this wont return a json file since integer value in the dict d, cannot be converted into json – prj Mar 02 '21 at 16:30
0

It looks like you're trying to serialize data that is numpy data of type int64. You'll need to convert data to python integers first.

Modifying the answers here should work: How to convert string values from a dictionary, into int/float datatypes?

EDIT: You can convert all entries to a string as follows:

import json

d = {'India': {'A': 1, 'C': 2}, 'Malaysia': {'B': 1, 'A': 1, 'D': 1}, 'Croatia': {'C': 1}}

for country_dict in d.values():
    for key in country_dict:
        country_dict[key] = str(country_dict[key])

y = json.dumps(d)
gazm2k5
  • 449
  • 5
  • 14
  • i need to convert int to str, i used for s in d.values(): for sn in s.values(): sn=str(sn) its still not converting – prj Mar 02 '21 at 16:29
  • It's the same principle as described in the solutions linked. Just change `int(sub[key])` to `str(sub[key])` – gazm2k5 Mar 02 '21 at 16:30
  • it still doesnt work. can we convert the dictionary d into a Multilevel dataframe and then convert it into json? – prj Mar 03 '21 at 04:41
  • I have edited my original response with code to convert the dictionary entries to strings. – gazm2k5 Mar 03 '21 at 13:55
  • this does not work either when this code is a method and if i am trying to 'RETURN' json, still gives the same numpy.int64 error – prj Mar 03 '21 at 14:27
  • Your function just has to `return y` at the end. I can't help any further without seeing the rest of your code, there must be int64s somewhere in the dictionary that you're trying to `json.dumps()`. – gazm2k5 Mar 03 '21 at 16:21