I recommend using the awesome requests library:
import requests
url = 'http://maps.googleapis.com/maps/api/directions/json'
params = dict(
origin='Chicago,IL',
destination='Los+Angeles,CA',
waypoints='Joplin,MO|Oklahoma+City,OK',
sensor='false'
)
resp = requests.get(url=url, params=params)
data = resp.json() # Check the JSON Response Content documentation below
JSON Response Content: https://requests.readthedocs.io/en/master/user/quickstart/#json-response-content
Answer from Zach Kelling on Stack OverflowI recommend using the awesome requests library:
import requests
url = 'http://maps.googleapis.com/maps/api/directions/json'
params = dict(
origin='Chicago,IL',
destination='Los+Angeles,CA',
waypoints='Joplin,MO|Oklahoma+City,OK',
sensor='false'
)
resp = requests.get(url=url, params=params)
data = resp.json() # Check the JSON Response Content documentation below
JSON Response Content: https://requests.readthedocs.io/en/master/user/quickstart/#json-response-content
The requests Python module takes care of both retrieving JSON data and decoding it, due to its builtin JSON decoder. Here is an example taken from the module's documentation:
>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r.json()
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
So there is no use of having to use some separate module for decoding JSON.
Python requests module with json data some fields are binary date
python - What's the best way to parse a JSON response from the requests library? - Stack Overflow
Why is the json information not loading in from the request ?
Help With Python, API GET Requests, and JSON
Videos
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.
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().
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.
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?
Starting with Requests version 2.4.2, you can use the json= parameter (which takes a dictionary) instead of data= (which takes a string) in the call:
>>> import requests
>>> r = requests.post('http://httpbin.org/post', json={"key": "value"})
>>> r.status_code
200
>>> r.json()
{'args': {},
'data': '{"key": "value"}',
'files': {},
'form': {},
'headers': {'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'close',
'Content-Length': '16',
'Content-Type': 'application/json',
'Host': 'httpbin.org',
'User-Agent': 'python-requests/2.4.3 CPython/3.4.0',
'X-Request-Id': 'xx-xx-xx'},
'json': {'key': 'value'},
'origin': 'x.x.x.x',
'url': 'http://httpbin.org/post'}
It turns out I was missing the header information. The following works:
import requests
url = "http://localhost:8080"
data = {'sender': 'Alice', 'receiver': 'Bob', 'message': 'We did it!'}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(data), headers=headers)
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'])
» pip install requests