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’])
didn’t work for me. i got error = string indices must be integers
Which line in the code did that error appear?
Hey Rowell can you assist with ORG , Network and ADDING DEVICES Python Scripts for Meraki.