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().
Converting Response to JSON?
How to Parse JSON Response in Python? - TestMu AI Community
[Python] How to parse JSon response from a post request (Requests)
Help With Python, API GET Requests, and 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?
I've tried a couple of different ways of doing this and there definitely is a response created by the post in which success: true but I cant find a way to output the data.
response = session.post('http://www.holypopstore.com/index.php', data=payload)
soup = bs(response.text, 'html.parser')
JSon = response.json()
print(JSon['success'])Hello, I am very new to Python and am trying to use Python to grab a JSON file from an API in order to extract data. I believe I have done the string concatenation properly, with the URL and Artist values I have created. If you visit the URL here you are able to see the JSON text correctly. However, I am trying to store this data in an object, however it does not seem to be working for some reason. Printing should yield the JSON text, but it does not.
Here is my code and the error I get when trying to run it. I'm really stumped at the moment because a lot of the tutorials I've seen seem to show these steps working fine.
» pip install requests