• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rowell Dionicio

Get Techie With It

  • Home
  • About
  • Resources
    • Archives
    • Book List
    • YouTube
  • Learn
    • DevNet Associate
    • PCNSA Certified
  • Blog
  • Contact
  • Show Search
Hide Search

json

Parsing JSON with Python

October 28, 2019 By Rowell 3 Comments

JSON seems to be a popular data structure to use with APIs. I work with Meraki often and noticed the decision they went with. You can see the output easily with an API dev environment such as Postman.

When working with the Meraki API, I receive data in the form of JSON. In my example of parsing JSON data into Python data structure, I’ll be querying the Meraki API dashboard to list all the organizations I have access to. This example leverages the Meraki Sandbox.

Using Postman to test my request, I copy the code to create Python script.

import requests

url = "https://dashboard.meraki.com/api/v0/organizations"

headers = {
    'X-Cisco-Meraki-API-Key': "secret-api-key",
    'Authorization': "Basic secret-auth-key",
    'User-Agent': "PostmanRuntime/7.18.0",
    'Accept': "*/*",
    'Cache-Control': "no-cache",
    'Postman-Token': "secret-postman-token",
    'Accept-Encoding': "gzip, deflate",
    'Referer': "https://api.meraki.com/api/v0/organizations",
    'Connection': "keep-alive",
    'cache-control': "no-cache"
    }

response = requests.request("GET", url, headers=headers)
print(response.text)

I’m going to skip over the details of how I use the API to connect to the Meraki Dashboard and focus on parsing the JSON response.

The way I’m getting the JSON data is by sending a get request to https://dashboard.meraki.com/api/v0/organizations

The response is assigned to the variable response and printed as a string using print(response.text). The output is:

[{"id":"578149602163689118","name":"Test Org [creation] 1551299469","url":"https://n27.meraki.com/o/Zq76qaB/manage/organization/overview"},{"id":"578149602163689494","name":"Your Momma's Network","url":"https://n27.meraki.com/o/OLu9EcB/manage/organization/overview"},{"id":"578149602163689487","name":"Your org","url":"https://n27.meraki.com/o/rrHTibB/manage/organization/overview"},{"id":"578149602163688579","name":"Your org","url":"https://n27.meraki.com/o/iVpEldB/manage/organization/overview"},{"id":"578149602163689106","name":"test 2","url":"https://n27.meraki.com/o/hDppEbB/manage/organization/overview"},{"id":"578149602163689090","name":"My org","url":"https://n27.meraki.com/o/niwmebB/manage/organization/overview"}
**truncated**

The presentation isn’t very useful and barely readable.

To make the data readable, I’ll utilize the JSON library within Python, used for encoding/decoding JSON.

Key to this library is when a string is loaded into the JSON decoder, it will convert JSON objects into Python dictionaries and JSON arrays into Python lists!

That’s crucial to remember. It gives us flexibility of iterating through the data. It had me confused for a while as I tried to figure out how to parse out the names of all the organizations into a single list.

First, we’ll need to add import json at the top of the script.

If I wanted to, I could make it easier to read with some sorting and indentation by adding print(json.dumps(r, sort_keys=True, indent=4, separators=(',', ':')))

The output is much clearer:

[
    {
        "id":"578149602163688921",
        "name":"Your org",
        "url":"https://n27.meraki.com/o/Lx9DRcB/manage/organization/overview"
    },
    {
        "id":"578149602163689112",
        "name":"Default Test Organization",
        "url":"https://n27.meraki.com/o/oHWaZbB/manage/organization/overview"
    },
    {
        "id":"578149602163689040",
        "name":"Lab 4",
        "url":"https://n27.meraki.com/o/CBPpjbB/manage/organization/overview"
    }
      ** TRUNCATED **
]

We’re printing the JSON data to the screen but we’re going to serialize that data using json.dumps and sort it with indentation.

Now we’re going to comment out print(response.text) since we no longer need the print out of the raw JSON format.

Below #print(response.text) we will turn the JSON data into a Python object and assign that to variable r:

r = json.loads(response.text)

What I’d like to do is parse the JSON data and only provide a list of Organization names I have access to.

At the bottom of the script I created a for loop to iterate through the list and only output the organization names:

for organizations in r:
    print(organizations['name'])

It took me a few hours to get this point as I learned how I took the JSON data and created a list of dictionaries.

The end result is below:

Your org
Default Test Organization
Lab 4
** TRUNCATED **

The final script is below.

import requests
import json

url = "https://dashboard.meraki.com/api/v0/organizations"

headers = {
    'X-Cisco-Meraki-API-Key': "secret-api-key",
    'Authorization': "Basic secret-basic-key",
    'User-Agent': "PostmanRuntime/7.18.0",
    'Accept': "*/*",
    'Cache-Control': "no-cache",
    'Postman-Token': "secret-postman-token",
    'Accept-Encoding': "gzip, deflate",
    'Referer': "https://api.meraki.com/api/v0/organizations",
    'Connection': "keep-alive",
    'cache-control': "no-cache"
    }

response = requests.request("GET", url, headers=headers)
#print(response.text)

r = json.loads(response.text)

# UNCOMMENT BELOW TO SHOW THE JSON OUTPUT PRETTIFIED
# print(json.dumps(r, sort_keys=True, indent=4, separators=(',', ':')))

for organizations in r:
    print(organizations['name’])

Primary Sidebar

Recent Posts

  • Passed Palo Alto Networks Certified Security Administrator (PCNSA)
  • 5 Years Running
  • Q4 2021 and Yearly Income Report
  • I PASSED JNCIA-MistAI
  • Admins and Role-Based Access Control – PCNSA

Categories

  • bschool
  • Certifications
  • Coding
  • DevNet Associate
  • Events
  • Lab
  • Networking
  • Personal
  • Podcasting
  • Professional
  • Reviews
  • Security
  • Short Stories
  • Uncategorized
  • Wireless

Archives

  • May 2022
  • January 2022
  • December 2021
  • November 2021
  • August 2021
  • July 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • November 2018
  • September 2018
  • August 2018

Copyright © 2022 · Written by Rowell Dionicio · You're awesome.