Handling simple Python/Json requests

Using the open weather map api as an example we can use the following steps in python to request some data from the api, which will return a json object. This is then parsed into a readable object that we can retrieve the data from that is of use to us.

1
2
3
4
5
6
7
8
9
import requests
import json

r = requests.get('http://api.openweathermap.org/data/2.5/weather?q=London') #return json object
json_data = json.loads(r.text) #convert object to dictionary

print json_data #will print the entire dictionary of objects

print json_data['sys']['country'] #parse the data you require from the object

It really is that simple!

^