2

I have the following variable in my python script:

response = {"tasks": [{"attachments": [{"id": "id123xyz", "type": "ElasticNetworkInterface", "status": "PRECREATED", "details": [{"name": "john doe"}] }]}]}

I am simply trying to return the value of status. However, I'm unable to do so.

1
  • 1
    You mean: print(response['tasks'][0]['attachments'][0]['status'])? Result: PRECREATED Commented Feb 15, 2022 at 7:09

3 Answers 3

1

You need to index into each dictionary and list that the status is inside.

You can do the following:

# Prints "PRECREATED"
print(response["tasks"][0]["attachments"][0]["status"])
Sign up to request clarification or add additional context in comments.

Comments

1

you need to access the dictionary and the list that is inside:

status = response['tasks'][0]['attachments'][0]['status']

#output
'PRECREATED'

Comments

1

In order to retrieve the status, you can do this

response['tasks'][0]['attachments'][0]['status']

If you would like to extract to dictionary or array of id and status then we can do:

dict([(attachment['id'], attachment['status']) for task in response['tasks'] for attachment in task['attachments']])

returns:
{'id123xyz': 'PRECREATED'}

1 Comment

This was Flawless. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.