We're all data people here, so you already know the scenario. It happens 1 to 100 times daily: You begin working with a new REST API, and it suits your needs perfectly. It's easy to pass requests, and responses contain all the information you've been looking for.
Still, there's just one problem: the complexity of nested JSON objects is endless, and suddenly the job you love needs to be put on hold to painstakingly retrieve the data you want, and it's 5 levels deep in a nested JSON hell. Nobody feels like much of a "scientist" or an "engineer" when half their day deals with key value errors.
Luckily, we code in Python! (okay fine, language doesn't make much of a difference here. It felt like a rallying call at the time).
Using Google Maps API as an Example
To visualize the problem, let's take an example somebody might want to use. The Google Maps API is a good candidate to fit the bill here.
While Google Maps is a collection of APIs, the Google Maps Distance Matrix. The idea is that with a single API call, a user can calculate the distance and time traveled between an origin and an infinite number of destinations. It's a great full-featured API, but as you might imagine the resulting JSON for calculating commute time between where you stand and every location in the conceivable universe makes an awfully complex JSON structure.
Getting a Taste of JSON Hell
We're going to use the Google Maps API to get the distance between two locations, as well as the estimated duration to complete the trip. Below we see how such a request would be made via Python's requests library.
We need to pass a few parameters:
- origins: Physical place (or places) representing where our trip begins. This value can be passed as a city name, address, or other formats; essentially what you'd expect from using the Google Maps app.
- destinations: Equivalent of the
originsparameter for trip destination(s)
- key: A Google API key
- transit_mode (Optional): Method of transportation for the trip (informs trip duration)
- units (Optional): Whether to return values as
As we're dealing with a single origin and single destination, the resulting JSON response for this request is relatively simple:
For each destination, we get two data points: the commute distance, and the estimated duration. If we hypothetically wanted to extract those values, typing
response['rows']['elements']['distance']['test'] isn't too crazy. I mean, it's somewhat awful and brings on casual thoughts of suicide, but nothing out of the ordinary
Now let's make things interesting by adding a few more stops on our trip:
A lot is happening here. There are objects. There are lists. There are lists of objects which are part of an object. The last thing I'd want to deal with is trying to parse this data only to accidentally get a useless key/value pair like "status": "OK."
Code Snippet To The Rescue
Let's say we only want the human-readable data from this JSON, which is labeled "text" for both distance and duration. We've created a function below dubbed
json_extract() to help resolve this issue. The function is intended to be flexible and agnostic, therefore can be imported as a module into any project you might need.
We need to pass this function two values:
- A complex Python dictionary, such as the response we parsed from
- The name of the dictionary key containing values we want to extract.
Regardless of where the key "text" lives in the JSON, this function returns every value for the instance of "key." Here's our function in action:
Running this function will result in the following output:
Oh fiddle me timbers! Because the Google API alternates between distance and trip duration, every other value alternates between distance and time (can we pause to appreciate this awful design? There are infinitely better ways to structure this response). Never fear, some simple Python can help us split this list into two lists:
This will take our one list and split it into two lists, alternating between even and odd:
Getting Creative With Lists
A common theme I run into while extracting lists of values from JSON objects like these is that the lists of values I extract are very much related. In the above example, for every duration, we have an accompanying distance, which is a one-to-one basis. Imagine if we wanted to associate these values somehow.
To use a better example, I recently used our
json_extract() function to fetch lists of column names and their data types from a database schema. As separate lists, the data looked something like this:
These two lists are directly related; the latter describes the former. How can this be useful? By using Python's
I like to think they call it zip because it's like zipping up a zipper, where each side of the zipper is a list. This output a dictionary where list 1 serves as the keys, and list 2 serves as values:
And there you have it folks: a free code snippet to copy and secretly pretend you wrote forever. I've thrown the function up on Github Gists, if such a thing pleases you.
That's all for today folks! Zip it up and zip it out. Zippity-do-da, buh bye.