简体   繁体   中英

How do I extract a nested alphanumeric value from a dictionary using Python

I have a dictionary as follows:

{
    'statusCode': 200,
    'body': '{"message": "data product request creation successful", "id": "a84f04f2539f11eca7ba0e2e68e2041f"}'
}

I want to extract the "id" inside the "body" key.

You'll want to have a look at the json module . Here is a basic example of how to do what you want:

import json

d = {'statusCode': 200, 'body': '{"message": "data product request creation successful", "id": "a84f04f2539f11eca7ba0e2e68e2041f"}'}
body = json.loads(d['body'])

print(body['id'])
# a84f04f2539f11eca7ba0e2e68e2041f

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