0

There is a dictionary like this:

    thisdict = {'12': [[1, 970000009], [3, 990000547], [9, 900000007]],
          '3': [[2, 970022209], [3, 990000547]],
          '32': [[4, 94042209], [5, 40547]],
          '6575': [[3, 94334009], [2, 923200547], [6, 7807], [7, 98807], [8, 8887]],
...
     }

How is it possible to save this dictionary into a json file?

I tried the below code but it does not work:

import json
with open('results.json', 'w') as fp:
    json.dump(thisdict, fp)

This is the error in output:

Object of type int64 is not JSON serializable

What is the solution to fix this error?

Umar.H
  • 22,559
  • 7
  • 39
  • 74
dxb
  • 273
  • 2
  • 13

2 Answers2

0

The code you posted works fine, and there's no obvious int64 in the part of the dict you've provided.

Reading between the lines, this looks like numpy data, so your problem is probably not with the dict values, but with the JSON serializer being unable to deal with numpy data types. There are already existing solutions to this problem you can use described here: Python - TypeError: Object of type 'int64' is not JSON serializable

Paul Mundt
  • 459
  • 6
  • 9
0

I rewrite your code, change datatype of dictionary and see same error.

import json
import numpy as np

thisdict = {'12': [[1, 970000009], [3, 990000547], [9, 900000007]],
            '3': [[2, 970022209], [3, 990000547]],
            '32': [[4, 94042209], [5, 40547]],
            '6575': [[3, 94334009], [2, 923200547], [6, 7807], [7, 98807], [8, 8887]]}
data = list(thisdict.items())
arr = np.array(data)

with open('result.json', 'w') as fp:
    json.dump(arr, fp)

TypeError: Object of type ndarray is not JSON serializable

I realize maybe your dictionary have a data type Int64. Please check type of thisdict.

Solution:

import json
import pandas as pd
import numpy as np

thisdict = {'12': [[1, 970000009], [3, 990000547], [9, 900000007]],
            '3': [[2, 970022209], [3, 990000547]],
            '32': [[4, 94042209], [5, 40547]],
            '6575': [[3, 94334009], [2, 923200547], [6, 7807], [7, 98807], [8, 8887]],
            }

data = list(thisdict.items())
arr = np.array(data)
df = pd.DataFrame(arr)
df.to_json('result.json')
Nguyễn Vũ Thiên
  • 761
  • 1
  • 9
  • 19
  • Thank you. Your code helps to solve it. print(type(thisdict)) command returns . There is thisdict[i] = x + y to create this dict. Does it mean that it is an array? If so, an array as you show in your code gives that error. What is the solution if an array like this structure should be stored in a json file? – dxb Dec 24 '20 at 11:24
  • Well, you need to add array to DataFrame of pandas, then convert it to json. ``` data = list(thisdict.items()) arr = np.array(data) df = pd.DataFrame(arr) df.to_json('result.json') ``` See full code here: https://ideone.com/qRQ5QF – Nguyễn Vũ Thiên Dec 25 '20 at 06:30