Since you're using requests, you should use the response's json method.
import requests
response = requests.get(...)
data = response.json()
It autodetects which decoder to use.
Answer from pswaminathan on Stack OverflowSince you're using requests, you should use the response's json method.
import requests
response = requests.get(...)
data = response.json()
It autodetects which decoder to use.
You can use json.loads:
import json
import requests
response = requests.get(...)
json_data = json.loads(response.text)
This converts a given string into a dictionary which allows you to access your JSON data easily within your code.
Or you can use @Martijn's helpful suggestion, and the higher voted answer, response.json().
Videos
Hi all, im trying to grab some data from a website using the requests module in Python. Here is what I have:
import requests, json
apiEndpoint = 'website.com'
r = requests.post(url = apiEndpoint)
data = r.json()
When I do a print(type(r)) I get <class 'requests.models.Response'>
When I do a print(type(data)) I get <class 'list'>
When I want to iterate through the values of data, I am getting an error.
for i in range(len(data['value'])):
for item in data['value'][i]['value_i_want']:do something
The error I am receiving is:
"TypeError: list indices must be integers or slices, not str"
So it converted the response into a list, when I want it to convert into JSON so I can access the data easily. Any ideas?