简体   繁体   中英

Parsing list of dictionaries in a dictionary to retrieve a specific key's value from each dictionary

I got a JSON response and converted it to a python dictionary using json.loads() . So the dictionary looks like this:

{u'body': u'[{"id":"1","entity":"zone","status":"PROCESSING","url":null,"createdOn":"2019-10-11T05:49:11Z"},{"id":"2","entity":"floor","status":"FAILED","url":null,"createdOn":"2019-10-11T05:49:15Z"},{"id":"3","entityType":"apartment","status":"SUCCESS","url":null,"createdOn":"2019-10-11T05:49:18Z"}]',u'isBase64Encoded': False, u'statusCode': 200}

I named this as testStatusList . I want to retrieve the value of "status" key of every dictionary inside "body" . I was able to retrieve the "body" by giving body = testStatusList['body'] . Now, the dictionary looks like:

[
    {
        "id": "1",
        "entityType": "zone",
        "status": "PROCESSING",
        "url": null,
        "createdOn": "2019-03-07T12:47:10Z"
    },
    {
        "id": "2",
        "entityType": "floor",
        "status": "FAILED",
        "url": null,
        "createdOn": "2019-08-19T16:46:13Z"
    },
    {
        "id": "3",
        "entityType": "apartment",
        "status": "SUCCESS",
        "url": null,
        "createdOn": "2019-08-19T16:46:13Z"
    }
]

I tried out this solution [ Parsing a dictionary to retrieve a key in Python 3.6

testStatusList= json.loads(status_response['Payload'].read())
    body = testStatusList['body']
    status =[]
    for b in body:
       for k,v in b.items():
           if k == 'status':
               status.append(v) 

but I keep getting AttributeError: 'unicode' object has no attribute 'items' . Is there a different method to get items for unicode objects?

So I basically want to retrieve all the statuses ie, PROCESSING, FAILED AND SUCCESS so that I can put an 'if' condition to display appropriate messages when something failed for that particular "id". I am very unsure about my approach as I am totally new to Python. Any help would be much appreciated thanks!

body is still a (unicode) string in your top blob. Use json.loads again on that string:

body = """[
    {
        "id": "1",
        "entityType": "zone",
        "status": "PROCESSING",
        "url": null,
        "createdOn": "2019-03-07T12:47:10Z"
    },
    {
        "id": "2",
        "entityType": "floor",
        "status": "FAILED",
        "url": null,
        "createdOn": "2019-08-19T16:46:13Z"
    },
    {
        "id": "3",
        "entityType": "apartment",
        "status": "SUCCESS",
        "url": null,
        "createdOn": "2019-08-19T16:46:13Z"
    }
]"""


import json
body = json.loads(body)
status =[]
for b in body:
   for k,v in b.items():
       if k == 'status':
           status.append(v) 
print(status)

Result:

['PROCESSING', 'FAILED', 'SUCCESS']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM