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