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