MongoDB with Async Python Transcripts
Chapter: Foundations: Pydantic
Lecture: JSON to Pydantic Converter
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
So we've seen that we can write classes that match a JSON document. Then it will parse those using the rules encoded through Pydantic into that class.
0:12
What if you already have the JSON? And what if it's complicated? Our little order example was quite simple, so I didn't mind typing it out.
0:20
What if you have more ugly, complicated data that you need to work with?
0:25
Over here on the left, we have this code generation section, and it shows you ways in which you
0:30
you can generate code from this using a CLI tool called data model code generator. Really
0:38
cool. Now, we can use this and you can run with it if you like. But let me introduce
0:45
you to a website that will do the same thing based on that, that code. JSON to Pydantic
0:52
converter. See on the left, we have a foo and a bar baz. It will generate this over
0:59
here on the right from Pydantic import base model. Our model is that this is an integer.
1:06
That's a string. Very cool, right? Let's try to hit that with our order data here that
1:12
we just wrote. So item ID and then I guess we could add in name is Michael. Remember
1:20
in JSON, you can't have single quotes. Look what we got over here. Item ID is a string
1:25
created date is a string. We could do better than that, couldn't we? So it's not exactly right.
1:30
This is detected as a list of strings. And really, that's because we've kind of passed in
1:35
failed bad data. So let's let it do like that. float in a string, we can make this optional,
1:41
right. But this is a pretty good jumpstart to writing these classes. Okay. Let's go with
1:48
something a little bit more complicated here. Let's take this weather service we have over We're at talk Python. This is live real weather.
1:57
Let's get the weather in. Let's say, where's that? That's in Portland right now. And notice how yucky this looks in Vivaldi or Chrome as well.
2:08
So Firefox to the rescue. There we have nice structured data. Now this is maybe too structured. Let's go to raw data and pretty print it.
2:18
And there we go. And look at that. I would say this is just about like heaven. 75 degrees, 44% humidity.
2:26
And if you prefer metric, that's 23 degrees Celsius. Over here, pretty print this. Now look at this, it's pretty complicated.
2:39
So we've got this weather thing embedded in here, then we've got wind reports and then units and then the forecast and the location
2:45
and the rate limiting. So let's go and put that into our JSON to Pydantic model and see what we get. A little more complicated here,
2:56
but we've got our weather, which is this section, our wind, which is this section, and notice, integer and float.
3:05
Maybe you should upgrade that to an integer, make it a little more obvious. Upgrade that to a float, rather. And finally, you've got your model.
3:14
Let's go ahead and just make sure that we can parse all of this, make sure that it works. Go back to PyCharm. Let's call that weather for a second.
3:26
Paste all of the code and this will be, let's call it weather model, right? This is the top level model.
3:32
It has some weather and wind and notice there's some really interesting aspects here.
3:38
So this itself is a pydantic model and the type of this is the pydantic model. So that means we can have hierarchical, structured,
3:49
hydronic models parsing and representing this, which guess what, for a document database like MongoDB,
3:55
is exactly the type of thing we're trying to model. So let's just go down here and say data equals and go grab our data we got here, like that.
4:06
And finally, say report equals weather model of star star data. You want that even more when it's this complicated and then print the weather is now.
4:21
And let's run this one. Look at how awesome that is. Weather is, now we have our member, this weather.
4:37
The weather is this weather object with broken clouds and category cloud and the wind is like this. Remember, even if this was off like that,
4:48
when we run it again, it's still gonna parse it, not because of the top level model, but because of the nested model into a float.
4:56
And there you have it, right? You could just print out the forecast if you want just the forecast, right? Use that hierarchy to get just the pieces
5:08
we're looking for. Excellent, excellent. So if you have complicated data, look and you already have it as JSON,
5:17
you can go to the json2pydantic.com converter or use the CLI tool down here to run it with the input and the schema and so on.
5:29
All right, however you want, you can start with your data and generate these Pythonic models right away.