Home > Software engineering >  How do I extract a nested alphanumeric value from a dictionary using Python
How do I extract a nested alphanumeric value from a dictionary using Python

Time:12-04

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.

CodePudding user response:

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

  • Related