简体   繁体   中英

Cannot convert Dictionary to JSON object using json.dumps() in python

I am trying to convert a dictionary using json.dumps()

def create_custom(json_input):
    custom = dict()
    custom['list'] = dict()
    custom['list']['Elements'] = json_input['nodes']
    custom['list']['links'] = json_input['links']
    return custom

JsonData = create_custom(json_graph.node_link_data(G))

for i, j in enumerate(Elements):
    JsonData['list']['Elements'][i]['Shape'] = j['Shape']

The above code is not complete but the final output I am getting is a dictionary

Output

{'list': {'Elements': [{'text': 'Task 1', 'Shape': 'Decision', 'id': 0},
   {'text': 'Task 2', 'Shape': 'Decision', 'id': 1},
   {'text': 'Task 3', 'Shape': 'Decision', 'id': 2},
   {'text': 'Task 4', 'Shape': 'Decision', 'id': 3},
   {'text': 'Task 5', 'Shape': 'Rectangle', 'id': 4},
   {'text': 'Task 6', 'Shape': 'Decision', 'id': 5}],
  'links': [{'source': 0, 'target': 1, 'key': 0},
   {'source': 0, 'target': 4, 'key': 0},
   {'source': 1, 'target': 2, 'key': 0},
   {'source': 1, 'target': 3, 'key': 0},
   {'source': 2, 'target': 1, 'key': 0},
   {'source': 2, 'target': 4, 'key': 0},
   {'source': 3, 'target': 1, 'key': 0},
   {'source': 3, 'target': 4, 'key': 0},
   {'source': 4, 'target': 5, 'key': 0},
   {'source': 5, 'target': 4, 'key': 0},
   {'source': 5, 'target': 1, 'key': 0}]}}

When I am converting the above output to JSON object

json.dumps(JsonData)

I am getting an error:

~\AppData\Local\Continuum\anaconda3\lib\json\encoder.py in default(self, o)
    177 
    178         """
--> 179         raise TypeError(f'Object of type {o.__class__.__name__} '
    180                         f'is not JSON serializable')
    181 

TypeError: Object of type int64 is not JSON serializable

I went across many answers but they are saying about numpy array etc.

Where I am going wrong

I could not recreate the error, but json.dumps worked fine for me. Please refer to the screenshot below:

代码示例和输出

Code I tried:

import json
JsonData={'list': {'Elements': [{'text': 'Task 1', 'Shape': 'Decision', 'id': 0},
   {'text': 'Task 2', 'Shape': 'Decision', 'id': 1},
   {'text': 'Task 3', 'Shape': 'Decision', 'id': 2},
   {'text': 'Task 4', 'Shape': 'Decision', 'id': 3},
   {'text': 'Task 5', 'Shape': 'Rectangle', 'id': 4},
   {'text': 'Task 6', 'Shape': 'Decision', 'id': 5}],
  'links': [{'source': 0, 'target': 1, 'key': 0},
   {'source': 0, 'target': 4, 'key': 0},
   {'source': 1, 'target': 2, 'key': 0},
   {'source': 1, 'target': 3, 'key': 0},
   {'source': 2, 'target': 1, 'key': 0},
   {'source': 2, 'target': 4, 'key': 0},
   {'source': 3, 'target': 1, 'key': 0},
   {'source': 3, 'target': 4, 'key': 0},
   {'source': 4, 'target': 5, 'key': 0},
   {'source': 5, 'target': 4, 'key': 0},
   {'source': 5, 'target': 1, 'key': 0}]}}
print(type(JsonData))
print(json.dumps(JsonData))
print(type(json.dumps(JsonData)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM