Home > OS >  Convert the existing nested disctionary output in string to a list to iterate over it
Convert the existing nested disctionary output in string to a list to iterate over it

Time:05-30

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

CodePudding user response:

You have to loop over the items of the dictionary.

for key, value in data2['roles'].items():
    res= value['name']
    print(res)

CodePudding user response:

There is nothing wrong with your code, I run it and I didn't get any error. The problem that I see though is your Json file, some commas shouldn't be there:

{
"count": 6,
"results": [
    {
        "key": "roles",
        "id": "1586230"
    },
    {
        "key": "roles",
        "id": "1586951"
    },
    {
        "key": "roles",
        "id": "1586932"
    } \\ here
],
"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"
    } \\ here 
},
"meta": {
    "count": 6,
    "page_count": 5,
    "page_number": 1,
    "page_size": 20
}

after that any parsing function will do the job.

  • Related