Accessing data from a JSON file
<p>I am quite new with JSON. My code consists in extracting data from a website wich requires a API key. Having extracted the information. I am trying to reach the information which is encoded in son through this format (here is a sample):</p>

<pre><code>[{"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}]
</code></pre>

<p>How do i access the different data in the json code ? This is the code i have come up with:</p>

<pre><code>import requests

reponse=requests.get('https://api.jcdecaux.com/vls/v1/stations/{station_number}?contract={contract_name}&amp;api_key HTTP/1.1')
</code></pre>

<p>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:</p>

<pre><code>print(reponse.headers)
print(reponse(2,/'latitude')
</code></pre>

<p>I am trying to access the information of lattitude in every element of the json code - the 2 represents the second element of the list and lattitude 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. </p>

<p>How do I fix it? I would like to access to all the value of each string of each member of the object 'response'.</p>

<p>UPDATE nÂ°1:</p>

<p>My new code is:</p>

<pre><code>import json
import requests
reponse=requests.get('https://api.jcdecaux.com/vls/v1/stations/{31705}?contract={Paris}&amp;apiKey={0617697a9795f803697de4b9abf9759d5406b3a0}  HTTP/1.1')
data = json.loads(response.content)
print(data)
</code></pre>

<p>However i get the error:</p>

<pre><code>Traceback (most recent call last):
  File "/Users/someone/Desktop/TIPE 2016:17/Programme TIPE 2016:2017.py", line 27, in &lt;module&gt;
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'
</code></pre>

<p>UPDATE nÂ°2:</p>

<p>my new code is : </p>

<pre><code>import json
import requests
reponse=requests.get('https://api.jcdecaux.com/vls/v1/stations/{31705}?contract={Paris}&amp;apiKey={0617697a9795f803697de4b9abf9759d5406b3a0}  HTTP/1.1')
data = response.json()
latitude = data[2]['latitude']
</code></pre>

<p>However i get the error:</p>

<pre><code>Traceback (most recent call last):
File "/Users/someone/Desktop/TIPE 2016:17/Programme TIPE 2016:2017.py", line 30, in &lt;module&gt;
latitude = data[2]['latitude']
KeyError: 2
</code></pre>

<p>Does it mean that response is empty ?</p>

<p>UPDATE nÂ°3:</p>

<pre><code>reponse.content
</code></pre>

<p>the answer is the following:</p>

<pre><code>b'{ "error" : "Unauthorized" }'
</code></pre>

<p>What is the problem ?</p>

<p>UPDATE nÂ°4:</p>

<p>my new code is :</p>

<pre><code>reponse=requests.get('https://api.jcdecaux.com/vls/v1/stations/{31705}?contract={Paris}&amp;apiKey={0617697a9795f803697de4b9abf9759d5406b3a0}  HTTP/1.1')

data =  json.loads(response.content.decode('utf-8'))
print(reponse.headers)
print(reponse.content)
</code></pre>

<p>the result is :</p>

<pre><code>{'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" }'
</code></pre>

<p>so the answer to my request is not empty but i do not have the authorization to access it. How can I solve this ?</p>

<p>FINAL UPDATE:</p>

<p>The new and working code is :</p>

<pre><code>import json

import requests

r = requests.get('https://api.jcdecaux.com/vls/v1/stations/31705?contract=Paris&amp;apiKey=0617697a9795f803697de4b9abf9759d5406b3a0')

response_json = r.json()

print (response_json['name'])
</code></pre>

<p>and the result is:</p>

<pre><code>31705 - CHAMPEAUX (BAGNOLET)
</code></pre>

<p>Requests has a builtin JSON decoder, so there's no need to use the json library:</p>

<pre><code>import requests

response = requests.get(url)
data = response.json()
</code></pre>

<p>Based on the details in your question, "data" <em>might</em> be a list of dicts that contain latitude.  So to extract the first one might be:</p>

<pre><code>latitude = data[0]['latitude']
</code></pre>

<p>But you'll probably want to play with the response interactively first to make sure.</p>

python json extract