Processing JSON resultsΒΆ

JSON stands for JavaScript Object Notation. It looks a lot like the representation of nested dictionaries and lists in python when we write them out as literals in a program, but with a few small differences (e.g., the word null instead of None). When your program receives a JSON-formatted string, generally you will want to convert it into a python object, a list or a dictionary.

Again, python provides a module for doing this. The module is called json. We will be using two functions in this module, loads and dumps.

json.loads() takes a string as input and produces a python object (a dictionary or a list) as output.

Consider, for example, the FAA’s REST API. If we request

result = requests.get("https://services.faa.gov/airport/status/DTW", params ={'format':'json'})

result.text will be a string that looks like this (though not as nicely formatted with indentations and line breaks).

{
  "IATA": "DTW",
  "ICAO": "KDTW",
  "city": "Detroit",
  "delay": "false",
  "name": "Detroit Metropolitan Wayne County",
  "state": "Michigan",
  "status": {
    "avgDelay": "",
    "closureBegin": "",
    "closureEnd": "",
    "endTime": "",
    "maxDelay": "",
    "minDelay": "",
    "reason": "No known delays for this airport.",
    "trend": "",
    "type": ""
  },
  "weather": {
    "meta": {
      "credit": "NOAA's National Weather Service",
      "updated": "4:53 PM Local",
      "url": "http://weather.gov/"
    },
    "temp": "39.0 F (3.9 C)",
    "visibility": 10.0,
    "weather": "Mostly Cloudy",
    "wind": "North at 12.7mph"
  }
}

Putting it all together, you can try putting this code into a file and executing it on your local computer.

import requests
import json
result = requests.get("https://services.faa.gov/airport/status/DTW", params ={'format':'json'})
d = json.loads(result.text)
print d['city']
print d['weather']['temp']

You should get a result like this (your temperature may vary!)

Detroit
39.0 F (3.9 C)

The other function we will use is dumps. It does the inverse of loads. It takes a python object, typically a dictionary or a list, and returns a string, in JSON format. It has a few other parameters. Two useful parameters are sort_keys and indent. When the value True is passed for the sort_keys parameter, the keys of dictionaries are output in alphabetic order with their values. The indent parameter expects an integer. When it is provided, dumps generates a string suitable for displaying to people, with newlines and indentation for nested lists or dictionaries. For example, the following function uses json.dumps to make a human-readable printout of a nested data structure. In fact, I used it to generate the printout above of the data about conditions at the Detroit airport.

def pretty(obj):
    return json.dumps(obj, sort_keys=True, indent=2)
Next Section - Unicode