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)]