Reputation: 69
I am quite new with JSON. My code consists in extracting data from a website which requires a API key. Having extracted the information. I am trying to reach the information which is encoded in JSON through this format (here is a sample):
[{"number":31705,"name":"31705 - CHAMPEAUX (BAGNOLET)","address":"RUE DES CHAMPEAUX (PRES DE LA GARE ROUTIERE) - 93170 BAGNOLET","latitude":48.8645278209514,"longitude":2.416170724425901},{"number":10042,"name":"10042 - POISSONNIÈRE - ENGHIEN","address":"52 RUE D'ENGHIEN / ANGLE RUE DU FAUBOURG POISSONIERE - 75010 PARIS","latitude":48.87242006305313,"longitude":2.348395236282807}]
How do I access the different data in the JSON code? This is the code I have come up with:
import requests
reponse=requests.get('https://api.jcdecaux.com/vls/v1/stations/{station_number}?contract={contract_name}&api_key HTTP/1.1')
I Believe that my request has formulated a response which lies in the "response" "folder" that has been sent by the website to my computer:
print(reponse.headers)
print(reponse(2,/'latitude')
I am trying to access the information of latitude in every element of the JSON code - the 2 represents the second element of the list and latitude the name of the value I am trying to access within the element of the JSON list. But I can't manage to do it. The error I get is a syntax error.
How do I fix it? I would like to access to all the value of each string of each member of the object 'response'.
UPDATE n°1:
My new code is:
import json
import requests
reponse=requests.get('https://api.jcdecaux.com/vls/v1/stations/{31705}?contract={Paris}&apiKey={0617697a9795f803697de4b9abf9759d5406b3a0} HTTP/1.1')
data = json.loads(response.content)
print(data)
However I get the error:
Traceback (most recent call last):
File "/Users/someone/Desktop/TIPE 2016:17/Programme TIPE 2016:2017.py", line 27, in <module>
data = json.loads(response.content)
File "/Users/someone/miniconda3/lib/python3.5/json/__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
UPDATE n°2:
my new code is:
import json
import requests
reponse=requests.get('https://api.jcdecaux.com/vls/v1/stations/{31705}?contract={Paris}&apiKey={0617697a9795f803697de4b9abf9759d5406b3a0} HTTP/1.1')
data = response.json()
latitude = data[2]['latitude']
However I get the error:
Traceback (most recent call last):
File "/Users/someone/Desktop/TIPE 2016:17/Programme TIPE 2016:2017.py", line 30, in <module>
latitude = data[2]['latitude']
KeyError: 2
Does it mean that response is empty?
UPDATE n°3:
reponse.content
the answer is the following:
b'{ "error" : "Unauthorized" }'
What is the problem?
UPDATE n°4:
my new code is:
reponse=requests.get('https://api.jcdecaux.com/vls/v1/stations/{31705}?contract={Paris}&apiKey={0617697a9795f803697de4b9abf9759d5406b3a0} HTTP/1.1')
data = json.loads(response.content.decode('utf-8'))
print(reponse.headers)
print(reponse.content)
the result is:
{'Content-Length': '48', 'Content-Encoding': 'gzip', 'Server': 'Apache-Coyote/1.1', 'Date': 'Fri, 23 Sep 2016 19:39:25 GMT', 'Connection': 'close', 'Content-Type': 'application/json'}
b'{ "error" : "Unauthorized" }'
so the answer to my request is not empty but I do not have the authorization to access it. How can I solve this?
FINAL UPDATE:
The new and working code is:
import json
import requests
r = requests.get('https://api.jcdecaux.com/vls/v1/stations/31705?contract=Paris&apiKey=0617697a9795f803697de4b9abf9759d5406b3a0')
response_json = r.json()
print (response_json['name'])
and the result is:
31705 - CHAMPEAUX (BAGNOLET)
Upvotes: -1
Views: 287
Reputation: 23243
You've messed up your url. I'm not sure what's about HTTP/1.1
suffix, but id definitely does not belong here. Also, all parameters in curled brackets looks off.
import requests
r = requests.get('https://api.jcdecaux.com/vls/v1/stations/31705?contract=Paris&apiKey=0617697a9795f803697de4b9abf9759d5406b3a0')
response_json = r.json()
print response_json
This code snippet prints:
{u'status': u'OPEN', u'contract_name': u'Paris', u'name': u'31705 - CHAMPEAUX (BAGNOLET)', u'bonus': True, u'bike_stands': 50, u'number': 31705, u'last_update': 1474660046000, u'available_bike_stands': 49, u'banking': True, u'available_bikes': 1, u'address': u'RUE DES CHAMPEAUX (PRES DE LA GARE ROUTIERE) - 93170 BAGNOLET', u'position': {u'lat': 48.8645278209514, u'lng': 2.416170724425901}}
To sum up, response_json
is now a standard Python dict, from which data may be accessed using standard dict protocol.
print response_json['status'] # prints OPEN
Upvotes: 1
Reputation: 1877
you could convert your json
data to dict and then access it like dictionary.
I believe it should be something like
data = json.loads(response.content.decode('utf-8'))
Upvotes: 1
Reputation: 95
Requests has a builtin JSON decoder, so there's no need to use the json library:
import requests
response = requests.get(url)
data = response.json()
Based on the details in your question, "data" might be a list of dicts that contain latitude. So to extract the first one might be:
latitude = data[0]['latitude']
But you'll probably want to play with the response interactively first to make sure.
Upvotes: 0