#100DaysOfCode in Python Transcripts
Chapter: Days 40-42: JSON in Python
Lecture: Request JSON data from an API
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
So, for this video, we're going to look at some JSON data that is returned by an API. Okay, we're not going to just download a file and parse that.
0:11
What I'd like to do is show you that a lot of APIs respond with JSON data, okay, JSON formatted data.
0:18
So, what we need to do in order to look through all of that is, first, we need to import json, obviously, so we can decode the data.
0:28
We also need to import requests, because that's how we're going to get the actual data. All right?
0:34
And, for later on in this video, we're going to get pprint, all right, and we'll discuss that in a bit. Now, there will be some magic here,
0:45
because I don't want you to see my API key, but, essentially, we're going to go r., r is requests, .get. Okay, let me do some magic here.
0:58
Copy and paste the key and get this JSON data pulled down, and then we'll carry on. All right, now I've just given us some white space
1:07
here to work with, get it out of the screen. So, r has been pulled down, the actual JSON has been pulled down, and we're going to assign that
1:16
to the data variable. Now, data = json.loads, all right? Loads allows us to actually decode the JSON output. Okay, so the JSON output is pretty ugly,
1:34
and using json.loads, that allows us to actually decode it, and make it readable. So, we're going to load in r.text, okay?
1:46
Now, if I was to hit data, and enter, and show you that, it would probably kill my session because there's so much data in this.
1:53
So what I've actually done is, I've copied and pasted this output into a text file for you. And that is here, okay? So you can see this is all the data
2:08
for my character in the game. Now, you can see the sort of nested dictionaries I was discussing before. Okay, you've got, at the highest level,
2:21
you've got a dictionary key there, and a value, and then we move on to the next one, and so on. Keep on filtering down, until we get to mounts.
2:30
Now mounts is just, long story short, mounts is a sort of creature you can ride on in the game to get around.
2:37
So, under the mounts key, we have a large dictionary here, called "collected," okay? And within that collected key,
2:47
we have our list of dictionaries as the value. So, you can see how far it drills down, so we've got the parent key here,
2:58
whose value is another dictionary, with the key to a list of more dictionaries, okay? And each mount, each animal that you ride on
3:11
in this output, has these keys, okay? So it's a whole array of data and it just goes on and on and on and on, and then you can see
3:23
number collected, we drop out of that list here, at the end, and we get back into that, we go up one level, and we see number collected,
3:32
number collected, number not collected, and then we go up to the parent level. I want to call it the parent level, the top level,
3:39
and we see realms and whatnot. Okay? So that is what that data looks like, the stuff we just pulled down, so how do we work with that data, okay?
3:50
Well, to look at that data, we need to treat it just like a dictionary, okay, there's a little bit of difference,
3:57
and we'll get to that in the next video, but, for example, we now know what the data looks like, so it allows us to figure out how we're going
4:05
to flesh it out in the Python code. So for item in data.items, I'm just going to do a standard, sort of, dictionary parse here.
4:16
We're just going to print item, okay, and watch what happens. We get all of that data here and it is, honestly, disgusting.
4:28
It does not format correctly, okay. Now, I've just scrolled that out of the buffer to get the distraction away. We can then go for item in data.items,
4:42
okay, same as we just did, but this time, we're going to use pretty print, or pprint, okay, and the beauty of pprint is that it actually
4:50
knows what JSON data looks like, and it formats it nicely for us. Okay, and this is the same output we saw in our Notepad file just then.
5:04
Wait for it to continue flooding my screen, probably overloading my computer, and it's going to look just like this.
5:12
Okay, so there it is there, and that's pretty much how I got that Notepad file in the first place. So this is JSON data.
5:20
This is importing it into Python, into your Python application, and this is pretty much printing the entire thing in a nicely formatted way.
5:29
So, in the next video, we will cover how to drill into this, but for now, have fun.