Python getting first item from requests reponse

I am querying an API using python requests like this…

url = "www.example.com" response = requests.request("POST", url)  response -------- [   {     "id": "485744",     "descript": "firstitem",   },   {     "id": "635456",     "descript": "seconditem",   },   {     "id": "765554",     "descript": "thirditem",   }, ] 

I am trying to access the first item in the response like this…

response = response.json print(response[0]) 

But I am getting the error…

TypeError: 'method' object is not subscriptable 

Where am I going wrong?

Add Comment
2 Answer(s)

json is a method of the Response class and not an attribute of the object. To get the json data, use:

response = response.json() 
Add Comment

You haven’t properly called it: response.json()

Alternate method:

import json #import this on the top response = json.loads(response) #You need to load the data into JSON 

ALSO: The additional commas (,) at the end of each "descript":

"firstitem", "seconditem", "thirditem" is not required and may give errors.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.