 Welcome back everyone. This is Brian and this is episode 26 continuing our journey into Python 3 and we're talking about JSON files. That's a little misleading. It doesn't actually have to be a file. What we're talking about is data And let me just post some JSON data here And I've got it in these triple quotes that way the IDE doesn't get all mad But really what we're looking at is this bit here Everything between the brackets. So we have the start and the stop and then we have key value pairs Now Jason can get a lot more complex than that But I wanted to keep it super simple because we're not really trying to learn the complexities of Jason We're just trying to learn how to use it with Python First off, why do you even need Jason? What is this? What do you need it for? Well, it is for app to app communications Now when I say communications, this could be anywhere from Network to remote calls to just simply writing and reading files Jason is meant to be a data format. So it's not necessarily a file format. You can do all of this Virtually across networks across memory. However, you want to do it, but it's going to greed upon format So let's say I write a program and you write a program and you want to be able to read what I'm creating We have to have an agreed upon format and that's what Jason really is As you might have guessed Jason can get a little bit complex. So before we deep dive in we need some imports Actually, we really only need one We're gonna import Jason makes it ridiculously simple. I know it's so complex But we need to import this module because we don't want to reinvent the wheel We want to use code that's already written and we know works. Let's also go ahead and make a variable I'm gonna call this file name. We're gonna use this again and again. I'm gonna call this Jason dot txt Let's start this off super simple here. So we're going to Put some dictionary to a Jason formatted string say that five times real fast. It's a little confusing But that's what we're gonna be doing here. I'm gonna make a variable called out D You can name it whatever you want, but I just want to know that this is the output dictionary This is what we're going to convert so I'm going to say the Dict function and I want to make a dictionary that has key value pairs of name Brian age 46 and That's Sick cat So we're gonna take this dictionary here and we're going to output that to a Jason format. So let's grab this A little bit of copy action there. I'm gonna say s equals and we want to use the Jason module and We want to dump S Not just dump now if your IDE may show Something a little bit different. You may see both dump and dumps with an s at the end We want the s which is short version for a string. So it's going to dump out a string I'm gonna feed it our variable there I'm going to actually Do the magic of copy and paste a little note there just in case somebody's confused about the difference between dump and dump s Usually when you get dumped it's a bad thing, but in this case, it's going to make us very happy All right, so that was actually politically sad We're going to say the string Is going to be our output Let's just run that see what it looks like there So sure enough string equals and look it looks like a dictionary This is what I love about dictionaries and Python in accord with Jason is they look virtually identical It's very easy to see that and see exactly what's going on because we have our key value pairs Let's go ahead and take that and push it out to a file Now remember I said as we get more complex with Python We actually write less code and I found that to be very true. It's one of the bizarre things about Python So I'm gonna say with open and we are going to Say file name our little variable there and we're going to write that And we're going to make a variable called that now we're going to say Jason Dump notice how there's two of them. There's dump and dump s whenever you see the s it stands for string. We want to dump this And what do we want to dump? We want to output our dictionary To the file And I'm going to put some notes in here just in case anybody gets a little confused as to what's going on there Very very simple very easy to understand. Let's go ahead and run this and it gives the same output now We have this Jason dot txt and if we open it up it has the string I should say stringified Jason or our actual dictionary in a Jason format It may look exactly like a Python dictionary and that's why people often get this confused. They go, oh, you're passing Python back and forth Actually, we're passing Jason and coded data. It just looks very strikingly similar to a dictionary object in Python Now let's do the exact opposite. I want to say from string. So we're going to go back up here and we're going to take this s variable right here Remember that Jason did a dump s or dump string to this little guy right here. So let's take him and I want to do the opposite of a dump which is a load I'm going to say nd people Jason in in case you're wondering about some of the names like dump. Yes, Python developers do tend to have a sense of humor So bear with me on some of these video and we're going to load s notice it's not load it's load s for string And we're going to feed it that string and I'm going to put some notes in here just in case anybody gets a little confused So load the dictionary from a string I have this named nd you can name this whatever you want doesn't really matter and I'm going to do the magic copy and paste here Let's go ahead and run this and you can see our dictionary is now loaded up very cool the way that works and it looks virtually identical So yes it will confuse a lot of people but rest assured we're working with Jason now in the spirit of doing things backwards we are now going to load this from a file So we're back up here where we said with open file in right text as f and then we did a dump and we dumped that dictionary out we're going to do the exact opposite of that And let's say with open and I want file name we're going to just read that as plain text as f and then let's make a variable we haven't used before so it's strikingly obvious that this is loading it So I'm going to say file dictionary or fd equals Jason and we want to load not load s because that would be a string we want load which is going to tell it load it from a file source And in case you're really curious know variable name does not matter I can actually name this person or P or Brian whatever I wanted to do does not really matter So now that we've loaded that well now comes complex part we just work with it it's really really that simple I'm going to say when I say complex I'm joking a little bit this is so ridiculously simple it's almost scary If you're coming from other languages and other frameworks you're kind of like what how is it this simple it's just mortifying so I'm going to call the type function here just so we can see what type what data type P is This is one of my little kind of grapes about Python is IDEs are great but if you just open this up in like a plain text editor and not an ID you may be looking at this trying to figure out what is load really doing what is it returning So the type function is going to print out or I should say give us back the data type there so we can print it out and then let's go ahead and print out the actual variable Let's run this again so type class dict so this is a class dictionary remember classes are something we're going to cover in the near future a class is a blueprint for an object so this is a dictionary data type and it is equal to you guessed it the information we just loaded back from the file which is now a dictionary Very simple very easy the main takeaway from this is Jason is a data format used to exchange data between applications this is an app to app communication data standard people all over the world different countries language barriers doesn't matter we'll say hey hand me a Jason file and it's very easy to work with When you look at it it is a string so it's very simple to just open this up in a notepad or text editor and just modify it if you need to but it's also extremely easy to work with at a programming level I hope you enjoyed this video you can find the source code out on github.com if you need additional help myself and thousands of other developers are hanging out in the void realms Facebook group this is a large group with lots of developers and we talk about everything technology related not just the technology that you need you just watched and if you want official training I do develop courses out on you to me.com this is official classroom style training if you go out there and the course you're looking for is just simply not there drop me a note I'm either working on it or I will actually develop it I will put a link down below for all three of those and as always help me help you smash that like and subscribe button the more popular these videos become the more I'll create and publish out on YouTube thank you for watching