[ad_1]
I have a rest api which provides a list of key value pair’s and we need to fetch all the id’s from this json output file.
Contents of the json file
{
"count": 6,
"results": [
{
"key": "roles",
"id": "1586230"
},
{
"key": "roles",
"id": "1586951"
},
{
"key": "roles",
"id": "1586932"
},
],
"roles": {
"1586230": {
"name": "Systems Engineer",
"deleted_at": null,
"created_at": "2022-04-22T03:22:24-07:00",
"updated_at": "2022-04-22T03:22:24-07:00",
"id": "1586230"
},
"1586951": {
"name": "Engineer- Software",
"deleted_at": null,
"created_at": "2022-05-05T01:51:29-07:00",
"updated_at": "2022-05-05T01:51:29-07:00",
"id": "1586951"
},
"1586932": {
"name": "Engineer- SW",
"deleted_at": null,
"created_at": "2022-05-05T01:38:37-07:00",
"updated_at": "2022-05-05T01:38:37-07:00",
"id": "1586932"
},
},
"meta": {
"count": 6,
"page_count": 5,
"page_number": 1,
"page_size": 20
}
}
The rest call saves the contents to a file called p1234.json Opened the file in python:
with open ('p1234.json') as file:
data2 = json.load(file)
for ids in data2['results']:
res= ids['id']
print(res)
Similarly
with open ('p1234.json') as file:
data2 = json.load(file)
for role in data2['roles']:
res= roles['name']
print(res)
failes with errors.
How to iterate over a nested array do i can only get the values of names listed in roles array
roles --> 1586230 --> name --> System Engineer
Thank you
[ad_2]