Dynamic handing of Meraki API results within Python like the prebuilt Meraki Google Sheet. (JSON)

LearningIsFun
Getting noticed

Dynamic handing of Meraki API results within Python like the prebuilt Meraki Google Sheet. (JSON)

Warning, I am new to coding so I might not explain this exactly correctly.  This is my limited understanding of it and question.

 

The Prebuilt Meraki Google sheet has specific code built into it to handle the returned JSON formatted information.  This dynamically goes through the results of a API call and builds out the Keys/Headers/Column Titles for the results of a call.  This works great for building out that table, basically goes through all the results, creates all possible Keys/Headers that could be needed, even if results vary and even if some Keys are not used in some of the results.

 

I am looking to see if there is existing code within Python that will accomplish the same results.  When using the Meraki Python Library the results come into python has a List of Dictionaries which can be nested several layers deep, each call returning different variations.  Sometimes this nesting is 2,3,4 layers deep and varies on the order of nesting. 

 

Does something exist within Python to handle that data dynamically like the JSON parser for the Google sheet?

I would ideally like to continue using the Meraki Python Library due to its built in features.  (Pagination, Rate control, etc).  With the consistent format the Meraki API delivers it's results I would have to think something like this already exists.

 

I have had the most success with using Pandas data frames but it doesn't break out the deeper nested layers automatically.   It also needs specifically rebuilt when using different calls to the Meraki API.

 

Thank you!!

1 Reply 1
sungod
Kind of a big deal

There are a generic methods (including some ready to use libraries) to 'flatten' JSON to key:value pairs, which you could then convert to row/column form.

 

But I don't really think that helps you as you'd just end up with umpteen columns named by the composite keys which you'd still need to interpret to make sense of the info.

 

When I extract data for reporting/analysis, I just write Python to pull out the bits I want from the nested structures, iterating through if necessary, and then output them as one row at a time in a standard column format of my choosing, that then feeds into downstream analysis routines.

 

Due to differences in the JSON structures returned depending on context (and sometimes null data), the code needs to be specific to the API call (and in some cases, the way the API call is being used and its target.)

 

From a quick look at the google sheet source, this is what it is really doing. It has specific output code for each API call it supports, i.e. it is not figuring it out dynamically, it is hard-coded.

 

See the code section titled "Reports"  https://gist.github.com/dexterlabora/9884aa4399715df8f453ea9de63255a0 

that's the code from around line 688 onwards (at least with current numbering.)

 

Get notified when there are additional replies to this discussion.