Consuming HTTP Services in Python Transcripts
Chapter: Reading JSON data with requests
Lecture: Demo: JSON from Python

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So here I have a little bit of Json text. So, this Json string, if converted to Json or to other objects has a demo field
0:11 and that says processing Json in Python, and the instructor is Michael and the duration is 5, presumably minutes.
0:18 So let's start working with this, let me just print this thing out for a second,
0:23 now, notice this is still running the old one so I am going to right click and run that and it prints it out and it's kind of indistinguishable
0:29 from say like a Python dictionary, which actually means working with Json from Python is super natural
0:35 because the mapping between Json as something in Javascript or text mapped over to Python as dictionaries is nearly one to one,
0:43 it's not exactly but very close, but let's just make sure that that is actually a string
0:48 so I'll say what is the type of this things as well, now you could see, okay it is a str, it is, so how do we load this?
0:55 Well, Python comes with what is described as 'batteries included' and that means it has a rich standard library including support for Json,
1:04 you could see that is coming straight out of the library, so we don't need anything external, we don't need to pip install anything, to work with this,
1:12 so what we are going to do is we are going to come over here and we are going to parse this Json so we'll say data,
1:18 so there is a couple of things we can do, we can load or we can load s, now, I think the naming choices here were super poor
1:29 how about load from string, or load from file, or something to that effect, but load you can see fp for file pointer and load s, s for string
1:37 so much for good variable naming, but the one we are looking for is we have some text in memory, we want to turn that from text in the form of Json
1:45 into Python dictionaries in the form of data, so we are going to use this and this is the one you are going to use most of the time
1:51 when you are doing services because you will make an http request and you will have text in memory, so text Json and then we can print this out again,
1:58 we'll just print out data and if I run this, you could see it looks really similar, it's different it has single quotes instead of double,
2:04 because that is the way Python dictionaries represent themself, and it's all in one line, again, because of that.
2:10 But if I gave you that string there, you couldn't really be sure that that didn't come from some sort of service,
2:16 so let's go and do type again, here we go, str says it's string, now it's a dictionary and you can see that actually the duration is a number,
2:27 it doesn't have quotes on it, and that is pretty cool, let's get a few pieces of information here,
2:32 we'll say in structure= now we want to get information, we want to get something on this dictionary, so that is standard Python,
2:38 you just do that by indexing in with the key so we'll say instructor and then I'll print your instructor is { }.format, maybe I'll even spell it right,
2:51 how about that? Now if we run it, you can see your instructor is Michael, now this is usually good, but sometimes this isn't so good,
2:57 so for example if this doesn't appear we are going to get something super bad,
3:02 we are going to get a KeyError, right, it sort of interspersed throughout here but somewhere in here, there we go, KeyError instructor,
3:09 so we can use a different format, different style of getting the value, so that is probably recommended because the internet you never know,
3:15 we can say get me an instructor like this and then it just says oh, your instructor is none, or we can even ply
3:20 a default here saying substitute if there is not instructor presented or we are just going hey look, you are getting a substitute teacher.
3:29 Alright, right now it's substitute, so if I go and put that back, now, hey your instructor is Michael, very cool, okay, so that is great,
3:35 final thing let's make a change to this data, let's say you know what, your new instructor is going to be Jeff, and somehow I want to
3:44 let's go and just print out the data and see that is changed, oops, don't need that twice, do we, we have your instructor is Jeff, okay,
3:53 so here we are processing this, instructor Jeff, but this is actually a Python dictionary, right, it's a dict,
4:00 we want this back serialized as Json, so how do we get this back, we'll say new Json and then we have a reverse of load s to dump s,
4:11 again, naming is really kind of unfortunate but it is what it is, you can dump Json to a file pointer, or you can dump s into memory
4:19 for the same reason that we frequently use the load s in services, we are going to use dump s in services as well,
4:25 and then let's just print out both the type of new Json and also new Json itself.
4:29 Let her run, boom, there it is at the end we have class str and now this is the Json, notice the double quotes and our class is still called the same
4:39 but our instructor is Jeff, same duration. So that is basically it, working with Json from Python,
4:46 really it comes down to the Json module, load s, dump s, and then somewhere in the middle, just working with straight up Python dictionaries,
4:55 one thing that may become some sort of hangup for you and there are ways to work around it,
5:03 with the Json module is that is there was some kind of date in here, this would not work so for example, if we come over here,
5:10 and this has a datetime like now, and we'll just put str time or something like that, if we try to run that, it's woow, datetime is not a serializable,
5:24 so basically, you can register handlers for serialization and you could also just do like a string representation,
5:32 so you could choose like what string or representation you want, and then we'll store it like that, okay.
5:37 But dates themselves are not supported in Json, just be careful of that. Alright, now you know about Json and working with it from Python,
5:45 let's go get it from somewhere way more interesting than an in memory static string, let's go get it from a web service, somewhere like gihub.

Talk Python's Mastodon Michael Kennedy's Mastodon