#100DaysOfCode in Python Transcripts
Chapter: Days 40-42: JSON in Python
Lecture: Parsing nested dicts in JSON

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Okay, so carrying on from the last video, we have this gargantuan bit of JSON response, and it's been formatted nicely by pretty print,
0:11 to look just like this. And what we want to do, the aim of this exercise here is to take the names of these mounts.
0:21 These in the game I've collected all of these mounts in this list here. In this list of dictionaries, and I want to get the list of them.
0:30 So how do we do that? Well it's easy enough using key and value calls for a dictionary, to get this data here, so we're not going to cover that.
0:39 What we want to do, is talk to the data that's nested below these lists and dictionaries. So we need to talk to mounts, the mounts key,
0:53 then we need to talk to the collected key, and so on down to what we want. So how do we do that, how do we do that in Python code?
1:02 Well the first thing we need to do is we need to start off our normal for loop. So we're going to do for item in data,
1:12 but instead of doing items as we normally would for keys and values, we're actually going to do some
1:20 tags, so the key that we want to look at specifically is mounts, isn't it? So let's just double check that.
1:29 Go back to the file, and sure enough yes it is mounts we want to talk to mounts. And this is where things are slightly different
1:37 and this is where things get a bit out of hand when it comes to handling nested dictionaries, you really have to investigate the JSON output, okay?
1:49 So we're going to go for item in data mounts, well let's just pprint item and see what we come up with. Well, we get collected, num not collected, and
2:03 number collected. And where did those come from? Let's go back to the file. So we have collected here, that's the first key, okay?
2:11 We're going to scroll all the way to the bottom, and then we have the next key, number collected and number not collected.
2:16 And that's exactly what popped up here. We just wanted the first key for mounts. But we want to drill into the rest of this stuff.
2:30 We want to get further into mounts. And this is where we further talk to collected. So you can see we are now going to start staggering
2:39 our way down, almost like a staircase. So now we're going to go for item in data mounts, and we know that's collected, okay, well what's going to
2:55 happen now? I'll give you a clue so we don't flood the screen. Printing item is going to print all of this stuff here,
3:06 and we don't want to flood our screen yet again, okay? What we do want however, is something in every single one
3:15 of these dictionaries, so in this list, we have this dictionary, which comes down to here for the Albino Drake.
3:23 And underneath that we have this dictionary for the Amber Scorpion which ends here. And we want the name; each one of these little dictionaries
3:32 has a name key. So we call 'em that. So we've drilled down now with our code to mounts and collected, and now we're going to start
3:43 talking to these keys here. So, we're not going to pprint item, we're actually going to pprint the items, but only the name tag.
3:58 So we've got for item in data mounts, we're drilling down from the top level to collected, and then pprint the item with just the name tag.
4:09 And watch this. There we have all of those mounts from the game, in the order that they are in those dictionaries,
4:21 and that's all we got, which is exactly what we wanted, okay? So we go back in here, you can see we got that name,
4:28 Albino Drake, Amber Scorpion, Argent Hippogryph. And so on. All the way down here. And it's really, really cool.
4:38 So that's how we've just worked our way down and passed our way down through the steps of the nested dictionary. So what can we so with this?
4:47 Let's make it interesting. Hop into this, now what's something that differs between these mounts?
4:55 Well this mount here, the Albino Drake is a flying mount because it's set to True. But the Scorpion is not flying. The flying is set to False.
5:08 And this is a boolean operator. It's just True or False. Not a string, if you considered it a string it would have had the quotes around it.
5:18 This is completely boolean, is aquatic and ground and everything. How 'about we just try and get the flying mounts,
5:27 the ones that are capable of flying through the air? Let's narrow it down, do something a little more usable.
5:33 So we're going to create a list first that we're going to throw these mounts in, so is flying, just create an empty list, alright?
5:44 Now we're going to use the same code for mount in data mounts, we're going to drill down to the next level again,
5:54 collected, well what do we want to do? We're going to say if mount is flying, we're going to do something.
6:11 Now note that we don't actually have to check for the truthiness so to speak, we don't have to do if mount is flying equals equals True,
6:23 because Python assumes True from default. And it is in a string so we don't have to try and match it with a string called True.
6:33 We can just go if mount is flying, if it is, then we're just going to append it. So is flying.append mount, alright? And that's it.
6:50 So what this has stored now, it's actually stored this entire dictionary. Each one of these dictionaries is now stored in the
7:02 is flying. What we can now do is talk to this thing, we can go Len is flying, we got 65 entries there. Let's compare that with here.
7:20 We know our collected has 204. I've collected 204 of these mounts in the game. But we only had 65 stored in here, 65 of these dictionaries stored.
7:33 So we know this worked. We know that it took just the flying mounts, the ones that are capable of flying.
7:40 So what we can do now, just to show you, we can go for i in is flying, print, we'll just keep pretty print just in case, pprint I, and you'll see
7:57 we actually got all of that data, didn't we? So we got the entire dictionary for each one of those flying mounts.
8:06 So again I'm sure you can guess, if we want just the data that we want, which is the name, we can go for I in is flying, pprint I, and then we use
8:22 the tag again that we want to take, name, and there we go. So our list has changed yet again, we no longer have
8:32 things like that Scorpion, it goes straight onto the next flying mount that I've collected. And that's it, so this is really really cool.
8:41 This is the best part about JSON. It's got all the data there, it's just really hard to get to sometimes, so using this method
8:49 you should be able to drill down through all of the sub dictionary or the nested dictionaries and find the data you want, and then you can


Talk Python's Mastodon Michael Kennedy's Mastodon