Python Tutorial for Beginners 40 - Working With JSON Data in Python

 


hey guys welcome to the next blog in Python tutorial for beginners in this blog I'm going to show you how to handle Jason values in Python so first of all what is Jason so Jason is a text format which stands for JavaScript object notation and Jason is a syntax which is used for storing and exchanging data now if you want to know more about Jason what is Jason and how to create Jason's and what value you can store in Jason then I have created a whole blog about that so you can watch that blog and understand more about Jason so let's get started with this tutorial and let's see how we can use Jason in Python so to use Jason in Python you use inbuilt package which is called Jason so you need to import this inbuilt package which is Jason now let's say I have a Python dictionary so let me create a dictionary in Python which have and let's say so this is our dictionary in Python so in order to convert this dictionary into Jason you use this Jason keyword which we have imported here and then call a method called dumps so this dumps method we can call here to convert some Python values to our Jason value and we will pass this dictionary variable a here and in order to print the value of Jason we can use this print method so I'm going to enclose this json dot dumps method inside our print function now let's run this code and you will see that this jason dodd dumps function has converted your dictionary into our jason value so this is our json text format now the next question which can arise here is what datatypes this dumps function takes as an argument so it takes a dictionary we already know now but what are the other data types which we can pass as an argument to this dumps function so it turns out that you can provide majority of your collections and your data types as an argument to this dumps function so you can provide dictionary here list tuple string int float or a boolean value any value you can provide here I think sets you cannot provide as an argument here but other than sets you can provide any other format as an argument to this dumps methods so let me show you how you can provide all these values into this json dot dumps function so here in the first print statement i have provided the dictionary in the second print statement i have provided the list in the third we have provided tuple in the fourth statement we have provided the string and then the integer and then float values and then we have provided the two boolean values false and true and at last we have provided this none keyword to this dumps function so let's run the code and let's see what happens so everything runs fine so that means you can convert from this JSON dot dumps function a dictionary or a list or a tuple or a string or a decimal or a float or a boolean value into our JSON value and all these are valid JSON values now let me go once again to this dictionary method and here also as the JSON values you can provide any type of values which I have shown here so you can use inside this dictionary for example another dictionary so I can just write so we can also provide a topper layer so let's run this code and it's totally valid so you can see our dictionary is now converted to a Jason value now this dumps function also takes few other parameters also so we can also provide a parameter called indent here so you need to write indent is equal to and then after that you can write some number here so if you want to give the indent of four to each and every element of your jason then you can write indent is equal to four and let me run the code once again and now you can see this jason is printed into a pretty format and the indentation here indentation means this indentation is for spaces for each and every element of your jason so if you want to print your jason into a pretty format then you can just right indent is equal to and the number of indents you want to give so let's say I want to provide just two indents here and let me run the code once again and you will see now this only have the two indents for every element of this jason also you can provide a parameter called separators and as the separators let's say first value i want to provide here is a dot value and the space here and the second value i want to provide here is this equals to symbol so what this separators argument does is it will replace your commas with this dot notation and your colons with this equals two notation and when i run the code you can see now the colon is replaced by the equals two symbol and comma is replaced by this dot symbol so you can use separators like this also but I will not prefer to use these kind of notation the original notation is the one which I prefer to use which is with commas and these coolants one more thing you can use here is the sort keys so you can just write sort keys here and never you ride through here it's going to sort your Jason according to their alphabetical order so you can see right now your keys are assembled in non alphabetical order so they are assembled in random manner but when you give sort keys is equal to true here and I'm going to run the code you can see H comes first then the marks and then the name and then the object so now your keys are assembled or sorted in the alphabetical order when you use this kind of notation now let's try to save this Jason value into a JSON file so in the last blog I have shown you you can use this width statement and then you can use open function and we can provide file name here for example demo dot Jason so jason file always have the extension dot jason and the second value we will provide here is the mode so we want to write to the file so that's why we have used w here as a fetch and then using this FH we can write to this file which we have created so we can pass this json dot dumps function here and now your dictionary a will be converted into a jason and then this jason will be saved into this demo dot JSON file you can see here i'm working inside this file handling project and here I only have dot py file and when I run this code you will see there is one more file which will be created here which is demo dot Jason so I can open this file here and here you will see all our JSON values are saved in this demo dot Jason file so we have provided these values into the demo dot JSON file let me just split the screen so you will be able to see it correctly so we have provided a name here so name is here age is here marks is here and then the object is he and also the pass is also here so this is how you can write your Jason values into a file now let's see how we can read the Jason values from a file so I'm going to remove this dictionary from here and instead of using this write mode I'm going to just use read mode which we use to read from the file and a file we already have so let's say we want to read all this data which we have written into this file and now instead of using this line I need to use FH dot read function here and to print the content what we have read from this file we can use the print function so I'm going to enclose this FX dot read into the print function and let's run the code first of all and when I run this code you can see it friends all the content of this jason now one thing which you need to observe here is this efetch dot read function is going to give you a string value so to confirm that we can just write type here and we will just see the type of this FX dot read function and I'm going to run the code let's see what's the answer so you can see this read replies you in the form of string so in order to convert this string into some possible format you use our function called jason dot loads so I'm going to save this string into our let's say a variable jason underscore str this is the variable name and we will assign whatever we read from the file into this variable and now what we can do here is we can convert the string into a jason value using a method called jason dot loads so let me create a variable once again so jason value is the variable name and then I can use this jason keyword which we have imported dot loads function so just use the loads function and then pass the string as an argument of this loads function so this loads function takes a string and converts into adjacent values so now let's print the type of the Jason values I'm going to just write print type and then we use the Jason value argument here and then once again run the code and you will see now the jason value data type is the dictionary so because now it's a dictionary we can pass some values from it so let's say from this Jason value dictionary we want to get the name so name here we have stored is max so we can just give the key here name and then run the code it's going to give us the value associated with the name which is max so this json dot loads function we use to convert from a jason or a string value into a Python dictionary so this is how you can handle Jason values in Python I hope you have enjoyed this blog and I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News