Getting Started with Meraki API using Python Part 3: Data Processing

Meraki Alumni (Retired)
Meraki Alumni (Retired)

Getting Started with Meraki API using Python Part 3: Data Processing

Getting Started with Meraki API using Python Part 3


Hi Everyone! Welcome to Part 3 of a series of posts to get you started with Meraki APIs using Python!


In the last part we saw how to use the Meraki library in Python to do a GET request in order to retrieve a list of organizations and how to securely use your API key, but we ended up with a response that is hard to read and process by just looking at it. We would need a way to be able to process the response, to do things like filter the list to look at specific organizations or maybe even get only certain fields from the list like the organization name and ID. In this post we will look at how to use python to process the response.


If you haven’t gone through Part 1 and Part 2 yet, please do so before proceeding.


So, at this point, we have the following output after issuing the GET list of organizations.





The text within the red box is the list of organizations. How can we process/filter this? 


First let us check what Python thinks of this response by using the type method.





Here we can see that Python see’s the response as a “list” datatype.


To keep it simple, a list in python is just a list of items, for eg:


[‘item 1’, ‘item 2’, ‘item 3’]


We can store this list in a variable so we can always refer to the list by name whenever we need it.





Now we have a list of items stored in a variable called items. Now, if I wanted just one of the items in the list I can call it by its index number. Please keep in mind, Python always starts counting from 0 instead of 1.


So each item has the following index value:





Of course, this is a small list. What if the list had a large number of items (like our list of orgs)?


One way to parse through this list is to use a “for” loop as follows:





So let us try the for loop with our list of orgs which is stored in response variable, we will print out the org ID and its name.





Here we iterate through each item stored in response (each item being an organization) and store it in a variable called org. Then we print out whatever is stored in org. Try it out yourself!


Here is the output:





Each item in response is now printed one line at a time. Great!


We can see each organization has some parameters such as id, name, url etc. We probably don’t want all of that, maybe just the name and id. How do we extract those? 


For that let us check what data type Python sees in each item stored in the variable org.





Here we use the same for loop to iterate through each item in response, but we now ask to print the type of data in the variable org using the type method, then we print the data stored in org as well, I have also added a print to add a line after each item so the output is easier to read.


Here is the output:





Here we can see that each item is seen as type class dict. What is that?


Dict refers to a “dictionary” which in Python is another data type. An example is shown below:





Here we have a variable called d_items and it has name, weight and height values. These are called key:value pairs, the keys being in this case name, weight and height, the values being X, XX and YY respectively. 


So what if I wanted just the name from d_items?





Simple! What we did here was called on the key called name here and used the print statement to print the value stored in ‘name’. Let us try this now with our list of organizations stored in response.





Here is the output:





Ha! Now we just printed out the name of the org’s! What if we needed the organization ID as well? Simply, we just add that to our print statement.





Cool, now we got the organization name and its ID!


So, in this way we just use the key name in our print statement to print the values stored in the key.


Now, what if I wanted to just print out the name and ID of only one org, not the whole list? No problem! Assuming you know what organization you are after, we can use if..else condition to do that.




Here we use an if condition. We say that if the organization name key has a value “DevNet Sandbox”, then we print the id and the name, otherwise do nothing. So in the output we see the organization id and name of DevNet Sandbox.


Now, lets try a more complex one, let us say we want the organization name and also the cloud region for the organization.





Aha! Now we have the organization name and its location! What happened here? Let’s look closely at a section in the print statement, org[‘cloud’][‘region’][‘name’]. If we look back at the time when we printed each organization as is, this is what we saw.





The cloud location is stored in ‘cloud’ and within that in ‘region’ and then within ‘region’, ‘name’. Or in other words it's nested as cloud -> region -> name. So to get to the value nested, we follow the hierarchy, print org[‘cloud’][‘region’][‘name’], simple!


Ok, now that we have seen how to process a response from the dashboard using for and if statements, I will leave it here and let you try and explore with this.


We will sign off here now, but look out for Part 4 in this series to continue the journey further!

Please leave your comments/questions/feedback below to help us continue this further. Also don’t forget to subscribe to Getting Started to get notified on the next post!

Meraki - To do something with soul, creativity or love.
5 Replies 5
Kind of a big deal
Kind of a big deal

You know how you say defining your API key in the source code is not recommended and then go on to do exactly that in training material designed for beginners?


I know this makes for an easier example - but what you are doing is training beginners to start a bad habit.


Otherwise - great effort!

Meraki Alumni (Retired)
Meraki Alumni (Retired)

Fair point, but the code used here is directly from the API docs, so basically one would do the same to test stuff(as I have done here using ipython), the API key used here is publicly usable. After testing, in the final code, as you say I wouldn't just store the key as a variable. 

Meraki - To do something with soul, creativity or love.
Here to help

As you're going deeper, you might want to introduce Pandas to convert the JSON output to CSV.

Meraki Alumni (Retired)
Meraki Alumni (Retired)

Thanks for the suggestion @Happiman-v2 ! It's a good idea, already in the roadmap 😊

Meraki - To do something with soul, creativity or love.

Im chiming in kinda late but what about using the pprint (pretty print) module in Python to make the printed response easier to read?  Any drawback to that?  Or was your intention purely to teach us some python?

Get notified when there are additional replies to this discussion.