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