ashakshan
ashakshan

Reputation: 479

accessing elements of a dictionary within a list

I have a dictionary like

elements = {
    "pipelineDescriptionList": [
        {
            "pipelineId": "df-09694461I15A2UPH0RZT",
            "name": "EQUIPMENT_DEV",
            "fields": [
                {
                    "key": "@lastActivationTime",
                    "stringValue": "2020-12-09T05:30:21"
                },
                {
                    "key": "@nextRunTime",
                    "stringValue": "2021-04-12T00:10:00"
                },
                {
                    "key": "@creationTime",
                    "stringValue": "2020-12-09T05:26:06"
                },
                {
                    "key": "@sphere",
                    "stringValue": "PIPELINE"
                },
                {
                    "key": "@healthStatusUpdatedTime",
                    "stringValue": "2021-04-11T00:15:22"
                },
                {
                    "key": "@scheduledStartTime",
                    "stringValue": "2020-12-09T00:10:00"
                },
                {
                    "key": "@healthStatus",
                    "stringValue": "HEALTHY"
                },
                {
                    "key": "@latestRunTime",
                    "stringValue": "2021-04-11T00:10:00"
                },
                {
                    "key": "@version",
                    "stringValue": "1"
                },
                {
                    "key": "name",
                    "stringValue": "EQUIPMENT_DEV"
                },
                {
                    "key": "@id",
                    "stringValue": "df-09694461I15A2UPH0RZT"
                },
                {
                    "key": "@pipelineState",
                    "stringValue": "SCHEDULED"
                },
             
                {
                    "key": "uniqueId",
                    "stringValue": "96AEF597-33FD-420A-8D39-D328EBE0EC1C"
                },
                {
                    "key": "@scheduledPeriod",
                    "stringValue": "1 day"
                },
                {
                    "key": "@firstActivationTime",
                    "stringValue": "2020-12-09T05:30:21"
                }
            ],
            "tags": []
        }
    ]
}

I need to access the stringvalues of keys @pipelineState, @nextRunTime, @lastActivationTime. I am currently accessing them by

elements['pipelineDescriptionList'][0]['fields'][0]['stringValue'] 
elements['pipelineDescriptionList'][0]['fields'][1]['stringValue'] 
elements['pipelineDescriptionList'][0]['fields'][10]['stringValue']

but my pipeline dictionary keeps changing, the index values for the same keys will differ for a different pipeline. for example:

elements = {
    "pipelineDescriptionList": [
        {
            "pipelineId": "df-0440555DDFN1Z0N4UOR",
            "name": "API_TOKEN_DEV",
            "fields": [
                {
                    "key": "@creationTime",
                    "stringValue": "2020-09-04T02:11:20"
                },
                {
                    "key": "@cancelActive",
                    "stringValue": "true"
                },
                {
                    "key": "@startTimestamp",
                    "stringValue": "2020-09-08T14:31:29"
                },
                {
                    "key": "pipelineCreator",
                    "stringValue": "AROAU2XMJTS2T67LQAZTF:[email protected]"
                },
                {
                    "key": "@version",
                    "stringValue": "2"
                },
                {
                    "key": "@id",
                    "stringValue": "df-0440555DDFN1Z0N4UOR"
                },
                {
                    "key": "@lastActivationTime",
                    "stringValue": "2020-09-08T14:31:29"
                },
                {
                    "key": "@nextRunTime",
                    "stringValue": "2021-04-12T02:11:00"
                },
                {
                    "key": "@lastDeactivationRequestTime",
                    "stringValue": "2020-09-05T02:09:29"
                },
                {
                    "key": "@sphere",
                    "stringValue": "PIPELINE"
                },
                {
                    "key": "@healthStatusUpdatedTime",
                    "stringValue": "2021-04-11T02:16:25"
                },
                {
                    "key": "@scheduledStartTime",
                    "stringValue": "2020-09-04T02:11:00"
                },
                {
                    "key": "@healthStatus",
                    "stringValue": "HEALTHY"
                },
                {
                    "key": "@latestRunTime",
                    "stringValue": "2021-04-11T02:11:00"
                },
                {
                    "key": "name",
                    "stringValue": "API_TOKEN_DEV"
                },
                {
                    "key": "@lastDeactivationTime",
                    "stringValue": "2020-09-05T02:11:29"
                },
                {
                    "key": "@pipelineState",
                    "stringValue": "SCHEDULED"
                },
                {
                    "key": "@firstActivationTime",
                    "stringValue": "2020-09-04T02:13:33"
                }
            ]}
    ]
}

How can I access the string values of my required keys without using indexes? Is there a way I can use key names directly to get stringValues ?

Upvotes: 0

Views: 74

Answers (2)

Bobby Ocean
Bobby Ocean

Reputation: 3294

Any solution is going to require you to search (i.e., iterate through all possible fields) for the solution you would like anyway. As such, you should probably eliminate that step by updating your fields.

for element in elements["pipelineDescriptionList"]:
    element['fields'] = {d['key']:d['stringValue'] for d in element['fields']}

Now you can access an item like:

elements['pipelineDescriptionList'][0]['fields']['@nextRunTime'] 

Upvotes: 1

Prune
Prune

Reputation: 77837

It seems that your main problem is a poor structure design. If you want to access the values by key, then change your structure to do exactly that:

elements = {
    "pipelineDescriptionList": [
        {
            "pipelineId": "df-09694461I15A2UPH0RZT",
            "name": "EQUIPMENT_DEV",
            "@lastActivationTime": "2020-12-09T05:30:21",
            "@nextRunTime": "2021-04-12T00:10:00",
            ...

This cuts out the silly numeric indices, and lets you access the desired information in a more natural way.

Upvotes: 0

Related Questions