Navigating and Extracting Specific Data from an API Response

Navigate the nested Python dictionary to access stock closing data for a specific date.

Learn how to efficiently navigate and extract specific data from complex Python dictionaries retrieved via APIs. This article provides practical methods for pinpointing valuable information using Python's built-in dictionary handling tools.

Key Insights

  • Use Python's built-in .keys() method to efficiently explore complex dictionaries retrieved from APIs, identifying primary keys like "Metadata" and "Time Series Daily" to access specific data.
  • Access nested data structures directly by using the date key within "Time Series Daily," allowing extraction of detailed daily information such as Apple's closing stock price of $217.90 on March 28.
  • Further analyze extracted datasets by importing dictionary data into a Pandas DataFrame, enabling easier visualization and manipulation for enhanced insights.

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

Let's take a look at how we might go about solving this question—this challenge. How do we print out the data for a specific date? Remember, this is all data we got back from an API—data we got essentially for free—and we can find all kinds of information in it. Let's give it a try.

If I just print out the data, or technically output the data, it's a very, very big dictionary. In fact, it's going to take me a while even just to scroll to the top. We can see there's just a massive amount of keys, values, and dictionaries in here. If you're not terribly used to navigating dictionaries, objects, and things like that, this might be a little tough for you, and you might feel a bit overwhelmed.

Fortunately, Python—and pretty much any programming language worth its salt—has some tools for quickly understanding this dictionary we're looking at. One of the helpful ones in Python is .keys(). If we look at that, it gives us just a list of the keys we could access here. "Meta Data" is one, "Time Series (Daily)" is the other.

Let's just print those out—keys in data. There it is. So let's take a look at what "Meta Data" is.

It's a dictionary, just like data is, and the keys are: information, symbol, last refreshed, output size, time zone. The value for each key is a string. We have information about it, and that's helpful metadata, but we want actual data—not data about our data, which is what metadata is. So the other option was "Time Series (Daily)."

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Let's try that one out. I was hoping to get some autocomplete with that. I didn't.

Let's try that. All right, this is, again, a lot of data—but maybe this is the data we want: this "Time Series (Daily)" object. Let me scroll up and up and up through all of this to get a sense of how it starts.

It looks like what we're looking at—data["Time Series (Daily)"]—is an object. It starts right with a curly brace, and each key is a date. The value for each date key is another dictionary. A dictionary within a dictionary, within another dictionary.

If we want a specific date, well, that looks like our next level down. Let's try the date we're looking for—and there it is. That's actually our answer for the challenge.

If we want to take it a step further, let’s say we want to actually find out, you know, what value did Apple close at on that day? Well, we can look at the next level, because the "Time Series (Daily)" key at this date is a dictionary, and it itself has keys—one of them is "4. close". And here we are. Here's our value: $217.90, which was the closing value for Apple on March 28th of this year. We can see all kinds of information in our API, and honestly, it’s given us so much valuable information that the challenge is really just navigating it at this point.

All right, that's our challenge. We'll go on to putting it in a Pandas DataFrame and looking at it from there.

Colin Jaffe

Colin Jaffe is a programmer, writer, and teacher with a passion for creative code, customizable computing environments, and simple puns. He loves teaching code, from the fundamentals of algorithmic thinking to the business logic and user flow of application building—he particularly enjoys teaching JavaScript, Python, API design, and front-end frameworks.

Colin has taught code to a diverse group of students since learning to code himself, including young men of color at All-Star Code, elementary school kids at The Coding Space, and marginalized groups at Pursuit. He also works as an instructor for Noble Desktop, where he teaches classes in the Full-Stack Web Development Certificate and the Data Science & AI Certificate.

Colin lives in Brooklyn with his wife, two kids, and many intricate board games.

More articles by Colin Jaffe

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram