0

I have a large nested dictionary with strings, floats and integers as data. I want to dump it into a JSON string, but a direct dump gives me an error:

json_dictionary = json.dumps(mapped_dictionary)


TypeError: Object of type int64 is not JSON serializable

So I tried to use this:

json_dictionary = json.dumps(str(mapped_dictionary))

This seemed to work at first but then I found out that the str function on the nested dictionary truncates the values after a certain point and then shows the rest of content. It's not dumping the entire nested dictionary, which is rather large.

I am wondering if there is a limit to the str function, and if so, what is a good workaround to have a nested dictionary with mixed type data to be dumped into a JSON object?

mlenthusiast
  • 1,094
  • 1
  • 12
  • 34
  • 1
    Ideally, you want to traverse the dictionary and convert each int64 to an int. Do not rely on `str()`. – DYZ May 07 '19 at 23:43

1 Answers1

1

As the error specified, JSON doesn't know how to serialize objects of type int64. Converting those objects to int should fix the issue. While I don't know the full scope of your problem, this should help.

Alassane Ndiaye
  • 4,427
  • 1
  • 10
  • 19