Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: JSON, who's that?
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
We saw that our API returns JSON, and this is not by coincidence, this is the most popular data exchange format on the Internet,
0:10
period, when one application talks to another, not just in Python, but across all the languages.
0:15
So let's just go play with this idea really quickly so that we know how to
0:19
work with it. So let's create a new thing, we'll call it "example_dict" for dictionary. We'll see where that goes
0:24
in a minute. I'm gonna use my fmain magic to find the program. It's gonna run here. So what we want to do is we want to talk
0:31
about this data structure called a dictionary, and a dictionary looks like this: it has curly braces, it has a key, so maybe this is gonna be city,
0:40
and then it has the value, the current value of the city. So this is like a variable type of thing,
0:46
and then the value might be Portland, and then the state, maybe the state is going to be Maine. And then, we could also have other,
0:56
more interesting things in here. Like we could have a details and the details could actually be a list of cold, snowy, winter, I don't know.
1:08
Maybe it's wintertime right there when we're doing this. So these are the things that we could have in there. In order to work with
1:13
this, first of all, if I just print this out, and run this, p, d, there we go, if we print it out, look how this looks.
1:21
See that right there? If we go back over to our API here and we click on our example,
1:25
we look the raw data, those are basically identical. The only difference that you'll find is
1:31
Python's representation have single quotes if you print it, JSON has double quotes. Other than that, they're basically the same. Yeah,
1:39
there's other technical small details like, for example, datetimes can't be represented in JSON documents where they can in Python
1:46
dictionaries. But, think of this JSON stuff as like a subset of these Python dictionary data structures. If I want to get something from it,
1:54
I can come over here and say like the city, we could print out just the city, notice Portland comes out.
1:59
If I asked for something that's not there, like country is not here, and I use the square brackets to access it,
2:04
I get a KeyError. There's nothing called country in here. So, oftentimes a better way to do this is come here and say "get",
2:11
so just do the brackets, if you say "get", you'll get either, you'll get the thing that you are asking for or none. You can even pass a default.
2:18
So if they don't ask, they don't provide a country, let's default it to the USA, and that way we'll get the default value here if
2:25
this does not exist, but if a country were to exist, like country is Germany, say now, Germany would come back, in Deutschland,
2:33
okay? And then also we can modify these things. We come over here and set the country to, set it to Australia, and then we could ask for it again.
2:42
Notice, now it's been set, and actually if we just print the whole thing out again, we get, over here our country is Australia.
2:48
Cool, right? What we can do is we can actually just take this, and it's better to take the pretty printed version just for your own well being,
2:55
and we can go over here and say, "the weather is, paste", you just indent it. But Python already understands that. So, if we want to get,
3:02
say, the weather, let's say the forecast and the temp. So first we've got to get the forecast and then we get this little baby internal
3:10
sub-dictionary back, which we can then ask for the temperature which will give us that. So we could do a print,
3:16
go to our weather and say "get the forecast", then we're gonna say "get the temp" and that should just print out 64.08 degrees
3:27
Fahrenheit, and it does. Okay, so this data structure is what we'll be exchanging with the
3:32
API, we're gonna be passing these JSON strings back and forth, but you'll see that Python has it, because they're basically subsets, sub-types,
3:42
the functionality of Python dictionaries, It's incredibly easy to convert that response over to a Python
3:48
dictionary, as we've explored right here, and then just directly worked with that.