|
| 1 | +# Coding 202: Parsing JSON using Python |
1 | 2 |
|
2 |
| -# Coding 202: Parsing JSON using Python # |
| 3 | +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](https://developer.cisco.com/site/cmx-mobility-services/ "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](#/labs/cmx/step/1). |
3 | 4 |
|
4 |
| -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](https://developer.cisco.com/site/cmx-mobility-services/ "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](#/labs/cmx/step/1). |
5 |
| - |
6 |
| - |
7 |
| -## Objectives ## |
| 5 | +## Objectives |
8 | 6 |
|
9 | 7 | Completion Time: 15 minutes
|
10 | 8 |
|
11 |
| -* Understand the basics of reading and parsing HTTP content using Python |
12 |
| -* Learn how to use Python to extract only the JSON data you want using the json library |
13 |
| - |
| 9 | +- Understand the basics of reading and parsing HTTP content using Python |
| 10 | +- Learn how to use Python to extract only the JSON data you want using the json library |
14 | 11 |
|
15 | 12 | ## Prerequisites
|
16 | 13 |
|
17 | 14 | You will want to make sure you have gone through the [Coding 101 lab](#/labs/coding-101-rest-basics/step/1 "Coding 101 Lab") if you are unfamiliar with Python and retrieving results from a RESTful service and the [Coding 201 lab](#/labs/coding-201-parsing-xml/step/1 "Coding 201 Parsing XML using Python Lab") for a similar approach to retrieving data using XML.
|
18 | 15 |
|
| 16 | +You should also have a basic familiarity with JSON. If you do not, consider visiting the [W3Schools JSON Tutorial](http://www.w3schools.com/json/ "W3Schools JSON Tutorial") to get a firm base to build upon. |
19 | 17 |
|
20 |
| -You should also have a basic familiarity with JSON. If you do not, consider visiting the [W3Schools JSON Tutorial](http://www.w3schools.com/json/ "W3Schools JSON Tutorial") to get a firm base to build upon. |
| 18 | +For this lab we will be using Python 3.4+. To verify your version run the following command in a terminal window: |
21 | 19 |
|
22 |
| -For this lab we will be using Python 3.4+. To verify your version run the following command in a terminal window: |
23 | 20 | ```
|
24 | 21 | python --version
|
25 | 22 | ```
|
26 |
| -If you are on a DevNet Zone station, the correct version of Python should already be installed. |
27 | 23 |
|
| 24 | +If you are on a DevNet Zone station, the correct version of Python should already be installed. |
28 | 25 |
|
29 |
| -## Step 1. Make a HTTP REST call with Python |
| 26 | +## Step 1\. Make a HTTP REST call with Python |
30 | 27 |
|
31 | 28 | To get started, let's create a simple Python script that can make a HTTP request to the CMX server.
|
32 | 29 |
|
33 | 30 | 1. Open the text editor and create a file named **get-ap-json.py**.
|
34 | 31 | 2. Save **get-ap-json.py** to the desktop.
|
35 | 32 | 3. Within the text editor add the following lines to **get-ap-json.py**:
|
36 |
| -``` |
37 |
| -from urllib.request import Request, urlopen |
38 |
| -import ssl |
39 |
| -req = Request('https://64.103.26.61/api/contextaware/v1/maps/info/DevNetCampus/DevNetBuilding/DevNetZone') |
40 |
| -req.add_header('Authorization', 'Basic bGVhcm5pbmc6bGVhcm5pbmc==') |
41 |
| -ctx = ssl.create_default_context() |
42 |
| -ctx.check_hostname = False |
43 |
| -ctx.verify_mode = ssl.CERT_NONE |
44 |
| -response = urlopen(req, context=ctx) |
45 |
| -responseString = response.read().decode("utf-8") |
46 |
| -print(responseString) |
47 |
| -response.close() |
48 |
| -``` |
49 |
| -In this snippet, we are: |
50 |
| - - Importing the required *Request* and *urlopen* libraries |
51 |
| - - Constructing a request to the CMX URI |
52 |
| - - Adding Basic authentication to our Request - the string of characters is the login/password encoded to Base64 |
53 |
| - - Opening the request and getting a Response back from the CMX URI |
54 |
| - - Parsing the Response as a String and printing it out to the console |
55 |
| -<br/> |
56 |
| -<br/> |
57 |
| -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 <a href="https://github.com/CiscoDevNet/coding-skills-sample-code/blob/master/coding202-parsing-json/get-ap-json-1.py" target="_blank">here</a>. |
58 |
| -5. Open a command prompt.<br/><br/> |
59 |
| - If your are using the Windows OS, type the following command into the command prompt window to move to your desktop: |
60 |
| -``` |
61 |
| -cd %USERPROFILE%\Desktop |
62 |
| -``` |
63 |
| - If you are using OS X, type the following command into the terminal window to move to your desktop: |
64 |
| -``` |
65 |
| -cd ~/Desktop |
66 |
| -``` |
| 33 | + |
| 34 | + ``` |
| 35 | + from urllib.request import Request, urlopen |
| 36 | + req = Request('https://devnetapi.cisco.com/sandbox/mse/api/config/v1/maps/info/DevNetCampus/DevNetBuilding/DevNetZone') |
| 37 | + req.add_header('Authorization', 'Basic bGVhcm5pbmc6bGVhcm5pbmc=') |
| 38 | + response = urlopen(req) |
| 39 | + response_string = response.read().decode('utf-8') |
| 40 | + print(response_string) |
| 41 | + response.close() |
| 42 | + ``` |
| 43 | + |
| 44 | + In this snippet, we are: |
| 45 | + |
| 46 | + - Importing the required _Request_ and _urlopen_ libraries |
| 47 | + - Constructing a request to the CMX URI |
| 48 | + - Adding Basic authentication to our Request - the string of characters is the login/password encoded to Base64 |
| 49 | + - Opening the request and getting a Response back from the CMX URI |
| 50 | + - Parsing the Response as a String and printing it out to the console<br><br> |
| 51 | + |
| 52 | +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](https://github.com/CiscoDevNet/coding-skills-sample-code/blob/master/coding202-parsing-json/get-ap-json-1.py). |
| 53 | + |
| 54 | +5. Open a command prompt.<br> |
| 55 | + <br> |
| 56 | + If your are using the Windows OS, type the following command into the command prompt window to move to your desktop: |
| 57 | + |
| 58 | + ``` |
| 59 | + cd %USERPROFILE%\Desktop |
| 60 | + ``` |
| 61 | + |
| 62 | + If you are using OS X, type the following command into the terminal window to move to your desktop: |
| 63 | + |
| 64 | + ``` |
| 65 | + cd ~/Desktop |
| 66 | + ``` |
| 67 | + |
67 | 68 | 6. In the command prompt window, type the following command to execute your newly created file using Python:
|
68 |
| -``` |
69 |
| -python get-ap-json.py |
70 |
| -``` |
71 |
| -7. When you run the Python script, you should get an screen full of XML data returned to the terminal. |
72 | 69 |
|
| 70 | + ``` |
| 71 | + python get-ap-json.py |
| 72 | + ``` |
| 73 | + |
| 74 | +7. When you run the Python script, you should get an screen full of JSON data returned to the terminal. |
73 | 75 |
|
74 | 76 | <div style="text-align:center" markdown="1">
|
75 |
| - |
| 77 | + <img src="/posts/files/coding-202-parsing-json/json-output.png" alt="JSON Command Prompt"> |
76 | 78 | </div>
|
77 | 79 |
|
78 |
| ----------- |
| 80 | +-------------------------------------------------------------------------------- |
79 | 81 |
|
80 |
| -Unfortunately, XML isn't the data format we wanted - but it is the data format CMX returns by default. Let's ask CMX to give us back JSON data in the next step. |
| 82 | +This dump of JSON data isn't that useful. Let's look at cleaning it up to understand it better in the next step. |
0 commit comments