Home:ALL Converter>Nested Dictionary JSON to Nested Dictionary in Python

Nested Dictionary JSON to Nested Dictionary in Python

Ask Time:2017-12-18T03:45:06         Author:CodePathLvl

Json Formatter

I have a dictionary of dictionaries in Python which looks like this:

  {      
   "Europe": {
        "France": (10,5),
        "Germany": (15,5),
        "Italy": (5,15),
      },
"North-America": {
        "USA": (20,0),
        "CANADA": (12,4),
        "MEXICO": (14,8),
       },
 }

I want to save the dictionary in a JSON file to get the data when I need it. I do that store like this:

with open(filename, 'a') as jsonfile:
    json.dump(dictionary, jsonfile)

The problem comes now. When I try to read the stored json dictionary I get same Error like this: Python json.loads shows ValueError: Extra data

The answer in that post is just to store the different dicts in a list and dumps all of them. But I dont understand how to do it if they are nested and they are created dynamically.

The way I read the json is this:

jsonFile = open(filename)
data = json.loads(jsonFile)
jsonFile.close()
return data

In resume. I need to load the dictionary from the json file to a dictionary in python. How can I achieve that?

Author:CodePathLvl,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/47858802/nested-dictionary-json-to-nested-dictionary-in-python
yy