15.1 The Source
15.2 Fetch the JSON data
Use the urllib2.urlopen(urlData)
method and pass it the URL.
Print the response code to make sure we're getting a 200. If so, call the .read()
method of urllib2 on the response and feed that data object into a function to use the JSON.
Otherwise print an error.
import urllib2 import json def main(): # define a variable to hold the source URL # In this case we'll use the free data feed from the USGS # This feed lists all earthquakes for the last day larger than Mag 2.5 urlData = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson" # Open the URL and read the data webUrl = urllib2.urlopen(urlData) print webUrl.getcode() if (webUrl.getcode() == 200): data = webUrl.read() # print out our customized results printResults(data) else: print "Received an error from server, cannot retrieve results " + str(webUrl.getcode()) if __name__ == "__main__": main()
15.3 Write a Function to Process JSON
Write a function to use the JSON results using methods of the json module. To see how this works examine the description of the json data on the USGS api.
def printResults(data): # Use the json module to load the string data into a dictionary theJSON = json.loads(data) # now we can access the contents of the JSON like any other Python object if "title" in theJSON["metadata"]: print theJSON["metadata"]["title"] # output the number of events, plus the magnitude and each event name count = theJSON["metadata"]["count"]; print str(count) + " events recorded" # for each event, print the place where it occurred for i in theJSON["features"]: print i["properties"]["place"] # print the events that only have a magnitude greater than 4 for i in theJSON["features"]: if i["properties"]["mag"] >= 4.0: print "%2.1f" % i["properties"]["mag"], i["properties"]["place"] # print only the events where at least 1 person reported feeling something print "Events that were felt:" for i in theJSON["features"]: feltReports = i["properties"]["felt"] if (feltReports != None) & (feltReports > 0): print "%2.1f" % i["properties"]["mag"], i["properties"]["place"], " reported " + str(feltReports) + " times"
json.loads(data)
loads the data into a dictionary (think hash table) of results. The variable containing the dictionary is a native Python object we can manipulate.
Consider print theJSON["metadata"]["title"]
the JSON structure is:
{ ... metadata: { generated: Long Integer, url: String, title: String, api: String, count: Integer, status: Integer } ... }
["metadata"]["title"]
is similar to dot notation in Javascript to drill down into the data.
print "%2.1f" % i["properties"]["mag"]
two digits and one decimal place of a floating point number from the mag node of the properties node of the json object of iteration i
.
15.4 Complete script
import urllib2 import json def printResults(data): # Use the json module to load the string data into a dictionary theJSON = json.loads(data) # now we can access the contents of the JSON like any other Python object if "title" in theJSON["metadata"]: print theJSON["metadata"]["title"] # output the number of events, plus the magnitude and each event name count = theJSON["metadata"]["count"]; print str(count) + " events recorded" # for each event, print the place where it occurred for i in theJSON["features"]: print i["properties"]["place"] # print the events that only have a magnitude greater than 4 for i in theJSON["features"]: if i["properties"]["mag"] >= 4.0: print "%2.1f" % i["properties"]["mag"], i["properties"]["place"] # print only the events where at least 1 person reported feeling something print "Events that were felt:" for i in theJSON["features"]: feltReports = i["properties"]["felt"] if (feltReports != None) & (feltReports > 0): print "%2.1f" % i["properties"]["mag"], i["properties"]["place"], " reported " + str(feltReports) + " times" def main(): # define a variable to hold the source URL # In this case we'll use the free data feed from the USGS # This feed lists all earthquakes for the last day larger than Mag 2.5 urlData = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson" # Open the URL and read the data webUrl = urllib2.urlopen(urlData) print webUrl.getcode() if (webUrl.getcode() == 200): data = webUrl.read() # print out our customized results printResults(data) else: print "Received an error from server, cannot retrieve results " + str(webUrl.getcode()) if __name__ == "__main__": main()