Coding 202: Parsing JSON using Python

In this Learning Lab you will learn the basics of parsing JSON content using Python. We will use the Connected Mobile Experiences (CMX) Mobility Services as a data source and query for the names of the available access points and put them into a simple list for display. If you want to dig deeper into CMX, review the Mobility Services Engine (MSE) Learning Lab.

Objectives

Completion Time: 15 minutes

  • Understand the basics of reading and parsing HTTP content using Python
  • Learn how to use Python to extract only the JSON data you want using the json library

Prerequisites

You will want to make sure you have gone through the Coding 101 lab if you are unfamiliar with Python and retrieving results from a RESTful service and the Coding 201 lab for a similar approach to retrieving data using XML.

You should also have a basic familiarity with JSON. If you do not, consider visiting the W3Schools JSON Tutorial to get a firm base to build upon.

For this lab we will be using Python 3.4+. To verify your version run the following command in a terminal window:

    python --version

If you are on a DevNet Zone station, the correct version of Python should already be installed.

Step 1. Make a HTTP REST call with Python

To get started, let's create a simple Python script that can make a HTTP request to the CMX server.

  1. Open the text editor and create a file named get-ap-json.py.
  2. Save get-ap-json.py to the desktop.
  3. Within the text editor add the following lines to get-ap-json.py:

    from urllib.request import Request, urlopen
    req = Request('https://devnetapi.cisco.com/sandbox/mse/api/config/v1/maps/info/DevNetCampus/DevNetBuilding/DevNetZone')
    req.add_header('Authorization', 'Basic bGVhcm5pbmc6bGVhcm5pbmc=')
    response = urlopen(req)
    response_string = response.read().decode('utf-8')
    print(response_string)
    response.close()
    

    In this snippet, we are:

    • Importing the required Request and urlopen libraries
    • Constructing a request to the CMX URI
    • Adding Basic authentication to our Request - the string of characters is the login/password encoded to Base64
    • Opening the request and getting a Response back from the CMX URI
    • Parsing the Response as a String and printing it out to the console

  4. Save the get-ap-json.py file. If you want to just download or review the current code, you can get it from GitHub here.

  5. Open a command prompt.

    If your are using the Windows OS, type the following command into the command prompt window to move to your desktop:

    cd %USERPROFILE%\Desktop
    

    If you are using OS X, type the following command into the terminal window to move to your desktop:

    cd ~/Desktop
    
  6. In the command prompt window, type the following command to execute your newly created file using Python:

    python get-ap-json.py
    
  7. When you run the Python script, you should get an screen full of JSON data returned to the terminal.

JSON Command Prompt

This dump of JSON data isn't that useful. Let's look at cleaning it up to understand it better in the next step.