JSON Tutorial in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back in today's video we're going to learn how to work with json files in python so let's get right into it all right now before we get into any of the code let's talk about why you should know how to work with json files the fact is that most apis be it for weather data be it for balance sheets for financial statements for stock data for any data actually if you're requesting something from an api chances are you're going to get back some json objects some json files so processing json objects and processing json files is very important for you as a programmer especially if you are into data science and machine learning and basically any field where you have to deal with a lot of with a lot of data coming in and going out but also if you're into cyber security you're going to deal with json files maybe for different purposes but you're going to have to know how to work with json files and even if those things are not what you're into even if you're not into cyber security you're not in the data science you should just know about json files because json files are a very basic way to to load data and to store data into those files so we're going to learn how to do this in python today and for this we only need to use the json module so there's nothing fancy about that we're not going to install anything we're just going to use the core python package called json uh and as an example right away let's say we have a simple dictionary here uh and in this dictionary in this dictionary we have some people so we're going to say people is the key here and people is just a list of people which themselves are dictionaries so they have for example a name uh and they have oh let's say the name is i don't know bob and then bob also has what's happening here come on bob also has an h for example 28 and bob has a weight of 80 kilograms for example uh and then you have another person here for example let's say this is name anna and anna has also an h let's say 34 and also weight let's say anna is i don't know 67 kilograms and we can now copy that and we can say okay let's add two more people here so let's just copy this here and paste it twice and let's change this to i don't know charles and this to daniel and there are 45 and uh 21 and the weight of daniel is 110 kilograms and charles is 78 for example so this is just a dictionary with some data and now let's say we want to store that dictionary now of course we already talked about this in the pickle video you can store that in a text file you can try to do i don't know some fancy string formatting and so on but the most intuitive way to store this is in a json file so what we can do here is we can just go ahead and say okay turn this dictionary into a string so i can save this string into a file so we're going to say json string is going to be json dot and now we have two functions we have dumps and we have lots dumps takes an actual json object and turns it into a string so by saying json dumps and then the dictionary what is it called my dict by doing that we get the json string and we can now go ahead and say okay with open my data dot json [Music] in writing mode this is not writing bytes mode because we're writing a string as f f dot write json string so by doing that we're saving this object into a json file let's run this and see what happens we have a json file here and this looks horrible why is that because we have everything in one line if we don't want that we have to specify the intense keyword or the indent keyword which is this one here and we can say for example two and if we oh indent like that and if we rerun this oh this is not the right function we need to do it in dumps function obviously indent equals two and if we now rerun this you can see that we have a better looking json file you can of course also set this to four if you want to then it's going to look a little bit differently there you go depending on what you prefer you can also try one and it's going to look like that it depends on what you prefer so this is how you write a basic or how you save a basic dictionary into a json file now let's do the other thing around uh or let's do it the other thing or uh let's do it the other way around uh let's get rid of all of this because we already have it in the json file here and let's load this json file into python and create a dictionary based on it so for this we're going to say with open my data dot json in reading mode sf we're going to say json object is going to be json.loads and as a parameter f read so basically we're getting the string from this file and based on that string we're creating a dictionary which is going to be the json object so we can now go ahead and say print json object and we're going to see that this is going to be the dictionary that we just exported as you can see here so i can go ahead and say people and i'm going to get the list and i can also say people zero so the first one and we can see bob h wait i can also go ahead and say okay give me the name whatever i can work with this dictionary even though i only have this json file here so this is how you load json files into python now let's look at a very simple object oriented example let's say we have a class person and this person or this class has a constructor and inside of that we have name age weight again [Music] and we have self.name equals name and we have self.h equals h and we have self.weight equals weight and then we have a function called print info where we basically just say print self dot name self.h and self.wait and then we have a simple function called get older with the parameter years and then we're just going to say self.h plus equals years just so we have some functions here and then what we want to do is we want to be able to export this person uh into a json file and to also load a new person from a json file now you can also do that with pickle if you want to actually serialize the the object but maybe you don't want to serialize it for python maybe you want to export it in json so that someone else can use it with a different programming language in a different context but you want to have the data so let's say you want to do that if you want to do that you can create a method called save to json or you can also say export to json whatever you want and of course you need to to provide a file name here and when it comes to json we already saw that we have two functions we have loads and we have dumps and basically you have either a string that you convert into a dictionary into a python dictionary or you have a python dictionary that you convert into a string no matter if it's actually a file no matter if it's from a web request you have a string or you have a dictionary and you can convert the one into the other one so in this case we have a person and in order to create a json file we would have to get the values as a dictionary so we can say person dict equals and now we have name is going to be self dot name age is going to be self.h and weight come on wait it's going to be self.wait now this dictionary can now be saved in a file so we can say with open um actually with open file name in writing mode sf we're just going to say uh f dot write json.dumps person dictionary there you go i think this should be fine and of course don't forget the indent because otherwise we're going to see everything in one line again and the other function or the other method that we're going to need here is load from json and of course if you have a second application for example a java application you would have to have the same structure so there you can have a class with person a class person with name h weight and you have the same it's a different code it's a different programming language that works in a different way but the json structure is the same so there you load it into a hash map or anything like that but you can just use json to export a python object and then import it into java for example so load from json again with a file name here and what we're going to do here is we're going to say with open file name in reading mode sf what we're going to do is we're going to say data equals json dot load f dot read so we get the string we get the json string we read that string and then we load loads actually not load from that string now what do we have here we don't have a problem and now we just have to say self dot name equals data name and self dot h equals data h and self dot weight equals data weight obviously and that's actually all we need to do right so now we can go ahead and create a new person for example p1 equals person we can call this guy mike we can say mike is 27 years old and weighs 90 kilograms and now what i can do is i can say p1 dot print info of course we can see then i can say p1 dot get older for example three years and then i can say p1 save to json so i can run this script and we have a problem here first position ah argument required let's say want to call this mic.json and you can see that he was 27 then he got older three years and in mike.json we can now see h30 we can see the mic object here now let's delete all of that and let's say p1 or p2 equals person with none on none this is not a best practice way to initialize uh an empty person or an undefined person but let's just do it for now and then p2 load from json mic.json p2 print info so if i run this now we can see mic 3090. so we can load files we can export files this is how you basically work with json and python remember you can either have a string that you turn into a dictionary or a dictionary that you turn into a string and this is basically what you need and if you work with stock apis if you work with weather apis with balance sheet apis you can use the same two functions dumps and loads in order to deal with those uh with those api responses so that's it for today's video hope you enjoyed hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 6,654
Rating: 4.9907832 out of 5
Keywords: json, python, python json, json tutorial, python json tutorial, python load json, javascript, javascript object notation
Id: jABj-SEhtBc
Channel Id: undefined
Length: 13min 22sec (802 seconds)
Published: Wed Aug 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.