Betafish
Betafish

Reputation: 1262

Parsing nested json without definite key name in python

I have a nested json as shown below. I am trying to get the values of city and label from the below json.

   {
      "5c7a3dd5-29e4-4f5b-9dc8-d2d451906cf1": {
        "id": "1d14eba1-de9e-4e53-9e9e-619432fd815b",
        "opinion_id": "3063e6ff-1483-4daf-8e80-41afa43b6348",
        "paragraph_id": "e6d419fe-5017-47f6-acf1-0e74034aed4a",
        "position": 7,
        "city": "Berlin.",
        "label": "Capital"
      },
      "0c0adcad-992f-4c30-aa58-7bf539f8d649": {
        "case_id": "6f6e70f7-3257-4f26-a8fa-8cf0e5f97920",
        "opinion_id": "71b435ff-112c-470b-a36b-c552e54a5605",
        "paragraph_id": "e6d419fe-5017-47f6-acf1-0e74034aed4a",
        "position": 3,
        "city": "Frankfurt.",
        "label": "Economic Capital"
      },
      "de968cdf-d865-4049-9f7d-f9a68be27432": {
        "case_id": "2d749a58-412c-4bc8-b268-7d5f7bec3d9a",
        "opinion_id": "1836c972-d6f7-420b-ba33-8f863cfca482",
        "paragraph_id": "e6d419fe-5017-47f6-acf1-0e74034aed4a",
        "position": 4,
        "city": "Paris.",
        "label": "Capital"
      }
    }

What i have tried is to loop through the json and extract the values on to a list of dict. Its throwing me an keyerror. I kind of understand. If the keys of the outerjson were same, then I could have used text = jsonData['KEY'][i]["city"]

with open(json_file_name) as input:
    jsonData = json.load(input)
    JSONLength = len(jsonData)
    data = []
    classes = dict()
    for i in range(0, JSONLength):
        text = jsonData[i]["city"]
        label = jsonData[i]["label"]
        print(text)
        print(label)
        data += [(classes[label], text)]

Upvotes: 0

Views: 748

Answers (3)

balderman
balderman

Reputation: 23825

try

data =   {
      "5c7a3dd5-29e4-4f5b-9dc8-d2d451906cf1": {
        "id": "1d14eba1-de9e-4e53-9e9e-619432fd815b",
        "opinion_id": "3063e6ff-1483-4daf-8e80-41afa43b6348",
        "paragraph_id": "e6d419fe-5017-47f6-acf1-0e74034aed4a",
        "position": 7,
        "city": "Berlin.",
        "label": "Capital"
      },
      "0c0adcad-992f-4c30-aa58-7bf539f8d649": {
        "case_id": "6f6e70f7-3257-4f26-a8fa-8cf0e5f97920",
        "opinion_id": "71b435ff-112c-470b-a36b-c552e54a5605",
        "paragraph_id": "e6d419fe-5017-47f6-acf1-0e74034aed4a",
        "position": 3,
        "city": "Frankfurt.",
        "label": "Economic Capital"
      },
      "de968cdf-d865-4049-9f7d-f9a68be27432": {
        "case_id": "2d749a58-412c-4bc8-b268-7d5f7bec3d9a",
        "opinion_id": "1836c972-d6f7-420b-ba33-8f863cfca482",
        "paragraph_id": "e6d419fe-5017-47f6-acf1-0e74034aed4a",
        "position": 4,
        "city": "Paris.",
        "label": "Capital"
      }
    }
    
city_n_label = [(x['city'],x['label']) for x in data.values()]
print(city_n_label)

output

[('Berlin.', 'Capital'), ('Frankfurt.', 'Economic Capital'), ('Paris.', 'Capital')]

Upvotes: 1

Maurice Meyer
Maurice Meyer

Reputation: 18136

Iterate over the dict's values:

with open(json_file_name) as input:
    jsonData = json.load(input)
    data = [(values['city'], values['label']) for values in jsonData.values()]
    print(data

)

Out:

[('Berlin.', 'Capital'), ('Frankfurt.', 'Economic Capital'), ('Paris.', 'Capital')]

Upvotes: 1

Ajay A
Ajay A

Reputation: 1053

Try this

with open(json_file_name) as input:
    jsonData = json.load(input)
    for k, v in jsonData.items():
        print(k) # prints dynamic key
        print(v) # prints its corresponding obj
        print(v['city']) # will print city of the particular key
        print(v['label']) # will print label of the particular key

Upvotes: 1

Related Questions