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