0

My JSON response is as follows:

[{"interface":"WAN 1","status":"Active","ip":"192.168.254.3","gateway":"192.168.254.1","publicIp":"206.59.240.69","dns":"192.168.254.1","vlan":2,"usingStaticIp":false}]

I wanted to extract the value of "status" but getting the below error:

TypeError: 'Response' object has no attribute '__getitem__'

Code snippet:

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)
print(response["status"])

Can somebody please help me to extract the value of 'status'?

1
  • looks like your response is an array. You would want to access the status property at the index you are looking for Commented Aug 29, 2019 at 15:37

1 Answer 1

1

You need to load the JSON first:

import json

response = requests.request("GET", url, headers=headers, params=querystring)
data = json.loads(response.text)
print(data["status"])

Note that based on the JSON response sample provided, the response is a list, in which case you would have to print data[0]["status"].

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Anoop. data[0]["status"] worked for me.

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.