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