Parsing JSON with Python

In this Learning Lab you will learn about JSON and how to parse it using Python

Objective

Completion Time: 35 minutes

  • Understand and use JSON data types
  • Learn how to parse JSON using Python

Prerequisites

Background

Python

  • To run the code samples, you need to have Python 3 installed on your machine.
  • If you are working on a DevNet Learning Lab PC at a DevNet event, Python 3.x + is already installed.
  • See How to Set up Your Own Computer section above for how to install Python on your own machine.

Clone Git Repo

  • If you are working on a DevNet Learning Lab PC at a DevNet event,
    • Open the Git Command window by clicking on the Git CMD icon on the Task Bar or click on the Start button, then in the Run bar type: git cmd and press the Enter key.
  • If you are working from your own workstation, on your desktop open a command terminal.
  • Go to the root directory by typing: cd \
  • Create a directory called 'C:\DevNetCode\yourname' by typing: mkdir DevNetCode\<your-name>
    • For example: mkdir DevNetCode\brTiller
  • Go to the new directory by typing: cd \DevNetCode\<your-name>
    • For example: cd \DevNetCode\brTiller
  • Clone the devnet-express-code-samples repository from GitHub. Enter the command below.
    git clone https://github.com/CiscoDevNet/devnet-express-code-samples
    


    Inside the directory you created you should now see directory 'devnet-express-code-samples'.



Step 1. JSON Basics

What is JSON

JSON which stands for Java Script Object Notation consists of text-based name-value pairs making it simple for applications to store and exchange data. It's designed to be lightweight and readable as well as minimal so there's not excessive text which has sometimes been the complaint of XML which is another text-based protocol for exchanging and storing data.

The JSON Structure

The structure and parsing of JSON is the same as Python dictionaries and lists.

In JSON data is set up in name value pairs just like a Python dictionary. However, in JSON dictionaries are referred to as objects. For example, {"car":"volvo", "fruit":"apple"} is a JSON object, but looks functions just like a Python dictionary. The object is assigned to a variable in the same manner as well: var={"car":"volvo", "fruit":"apple"}. In addition, JSON data is accessed the same way as when accessing data in a Python dictionary. For example, to get the value for fruit in Python we would enter var["fruit"] which would return apple. To display this value we enter print(var["fruit"]) . We can also loop through and display all of the keys and values.


var={"car":"volvo", "fruit":"apple"}
print(var["fruit"])
for f in var:
  print("key: " + f + " value: " + var[f])



As with Python, JSON also uses lists which it refers to as arrays. In JSON an array is typically nested in an object. For example, {"donut":["chocolate","glazed","sprinkled"]} . Let's assign this JSON object to a variable: var1={"donut":["chocolate","glazed","sprinkled"]}. Notice here that donut is the key, and the value is the array of the donut flavors. If I wanted to get the chocolate donut, (you're getting hungry aren't you?), I would access it by entering var1["donut"][0] which would return chocolate because it is the first element in the array. I could display the text by entering print(var1["donut"][0]). We'll loop through and display the values too.


var1={"donut":["chocolate","glazed","sprinkled"]}
print(var1["donut"][0])
print("My favorite donut flavors are:", end= " ")
for f in var1["donut"]:
    print(f, end=" ")



Give it a try!

  1. Open a terminal and go to the directory created back in the prerequisites section named: DevNetCode\<your-name>
  2. Go to directory module00\00-prep-05-python-json. In the terminal type: cd \DevNetCode\<your-name>\devnet-express-code-samples\module00\00-prep-05-python-json where <your-name> is the name you gave the directory.

Run and Edit the Python Script

  1. Run the python script called json_parse_1.py and check the results.
  2. Modify the file json_parse_1.py.
    a. Write code to print out one value and then all values of each JSON structure
  3. Save the file, run the script.
  4. After you've completed the assignment compare your solution to Python script json_parse_1_sol.py.

Next Step: Parsing More Complex JSON Structures