#100DaysOfCode in Python Transcripts
Chapter: Days 40-42: JSON in Python
Lecture: Concepts: what did we learn
0:00 Okay, so that was JSON.
0:02 I hope you enjoyed it.
0:03 I know it seemed a bit basic, but the reality is
0:07 that's most of the work with JSON
0:09 that you'll end up doing, right,
0:10 just passing the code, passing the output to find
0:14 the tags that you need and pulling it out really.
0:17 So let's recap everything we did.
0:21 For the first bit of code, it was just pulling down
0:24 and viewing the JSON files.
0:26 We just had a sort of overview of what it looked like,
0:29 the format and what we were looking for and so on.
0:33 So we begin by putting JSON, okay.
0:36 Then we pull down the file using requests.
0:39 Then we used json.loads.
0:43 And this essentially decodes the JSON code that is,
0:48 or the JSON output that's in the odd.text.
0:52 Okay so the request pulls down the JSON output
0:55 and then json.loads decodes it,
0:58 makes it readable for our code, okay.
1:01 Then we just handle the data similar to a dictionary, okay,
1:06 and we saw that if we would adjust at the top level
1:09 for the output that we had, if we had just run
1:13 standard four loop iteration over the data,
1:17 we got spammed with a lot of data, okay.
1:21 Now that was because we were just doing
1:23 a standard print, yeah, so it didn't format it at all.
1:28 Now that's why we were importing it,
1:31 the top there you can see from pprint import pprint
1:34 and that is pretty print.
1:36 And that formats our JSON decoded output
1:40 in a really nice way.
1:42 Pretty much the way that you would expect to see it
1:44 if you had an in-browser JSON decoder or JSON viewer.
1:49 And this is a nice way of presenting it
1:51 on our Python command line.
1:54 Then we had a look at the JSON nested dictionaries.
1:57 This is where things get a little more complicated
2:00 and you can definitely see that with the data
2:03 that we had.
2:04 So when we looked at our dataset, we saw that
2:07 there was a mouse key sitting there, right.
2:09 So to iterate over that, we run four item in data mounts
2:15 and then we p print the item.
2:17 And that got our next level of keys.
2:23 And specifically we then had a collected key
2:27 which then underneath that had another dictionary
2:30 and list below.
2:31 Okay, so we stared working our way down
2:35 through the hierarchy of dictionaries,
2:38 through the nested dictionaries.
2:40 Next from there we found we wanted to look for
2:44 the specific names of the mounts
2:47 and that's where we used the name key.
2:49 We specified that in the tag, okay.
2:52 And that p printed a really nice list of names for us.
2:56 Then we challenged ourselves to sit there
2:59 and go okay out of all the mounts
3:01 that we had collected, let's just print out
3:05 the ones that are considered flying, okay.
3:08 And that's what this bit of code here does.
3:11 We have an empty list called IsFlying
3:14 and then it goes through, checks to see
3:16 if IsFlying is True, and remembering Python,
3:20 you don't necessarily need to keep saying
3:23 if something is true.
3:25 You can just say if something,
3:27 and that is True in itself.
3:30 So if mount IsFlying, then we add the mount data,
3:34 all of it, we add all of that mount data
3:36 to the IsFlying list.
3:39 And then we can just iterate over that
3:41 as we so pleased.
3:45 So this is the JSON data example
3:48 that we've just gone over just as a little reminder for you.
3:52 It was quite in depth.
3:54 It just dug deep a little bit, okay.
3:58 You could see now everything if you've forgotten already,
4:01 everything sort of makes sense.
4:05 So now it's your turn.
4:06 So what next?
4:07 Well for this one, there is actually something
4:10 very specific I'd like you to try.
4:12 You're welcome to do whatever you want with JSON obviously,
4:15 but I think a really good challenge for you
4:18 at this point would be to go to our Code Challenge platform
4:24 and just look at this code challenge on
4:29 Code Challenger Number 16.
4:30 Query your favorite API, okay.
4:34 Now in this challenge we ask you to go out
4:37 onto the internet to your favorite API
4:39 and just do anything, okay.
4:41 Do any sort of a pull as long as you're querying the API.
4:45 But as we've discussed, APIs tend to return
4:48 a lot of JSON data.
4:50 So a specific one you could try
4:52 rather than going through all of these
4:54 looking for one that might interest you
4:55 is the OMDB API.
4:59 If you click this OMDB API link
5:02 from our Challenge 16, you end up on this website here,
5:07 OMDB Open Movie Database, okay.
5:10 And it actually returns JSON data
5:14 and you can do some examples here
5:16 and see what that will look like for you.
5:19 So if you want something to challenge yourself
5:21 for day three, go through this here, OMDB,
5:24 query the API, and see what you can do
5:27 with the return data.
5:29 Just play with it, manipulate it,
5:31 just like we did with our other code.
5:33 Maybe turn it into an app of some sort.
5:36 Just whatever you have time for
5:38 and whatever you're willing to try.
5:40 And other than that, keep calm and