Python JSON Parsing: A Step-by-Step Guide to Extract Data from JSON

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video we are going to learn how can you extract data from Json file how the Json file looks and how to fetch the data from the Json file everything step by step we are going to learn [Music] in this video we have learned how can you convert the Json data which is coming from the server into this format now our focus is we saw the data however I do not need the entire data I only need a specific information out of the entire data for example I would like to only print this description the description of the current weather broken clouds I only wanted to print the current temperature of the city right how can I fetch only the specific data what I need in my python code that is something we are going to learn all right now here our main goal is to fetch specific data from Json clear this is what we wanted to do in this video so let's see what is the code and how to write the code now for this you need to First Target what is the variable which contains your data pretty data is nothing but it is just formatting your data but which is the variable which contains our actual data is Json underscore data where we are converting the information that we have received from the server into Json format here now this is the variable which contains my actual data right for example just to recall I will simply print print Json underscore data if I print this how we have got the output do you recall it has come in a single line remember this is this is the print okay so here you can see it is coming in the single line just to read the data in a proper way to make it human readable we have converted this code into pretty format so these two lines of code are actually not needed in in the code but required when you have to really understand where the data is that is why we have done a pretty format now here our goal is to fetch the specific data right so as I said we have to deal with this Json data variable which data variable is very important because many will start using this pretty data pretty data won't help you you have to work on the variable which contains your Json data so this is our primary variable okay all right now how do I fetch it now to fetch something let's create a variable let's say I want to fetch the description so I'm going to create a variable called d equals to I want to fetch the description now tell me which variable contains your data actual data is Json underscore data so I'll first write this variable Json underscore data in Python the variables are case sensitive so remember to write in the same way so I got the Json data now in the Json data what are the things available entire data is there you if you see all this data is there inside my Json right until the end until here right so everything is contained in this design data or in the single line if you see this is the data which contains everything I have to pull a specific information so let's target this description okay description where it tells me what is the current weather conditions broken clouds okay so I have to Target this description now for this first you wrote the Json data variable which contains everything then I'll use square bracket now the square bracket what contains weather right so I'll say weather I will teach you step by step you just see how it is working so I'm saying whether copy and I'm going to paste weather now let's say I am interested to see the data so I am going to say print B okay let's run it I'm going to remove the other uh let it be there so let me run this let's enter some other name let's say Bengali now what is happening when you started printing here how many print the first print is the pretty data until here it is the pretty data now here if you see I am printing B so if I print D what is the output I have got let me copy this to a notepad so that we understand in a better way now if I look at my notepad what happened it has fetched the entire data within the square bracket ID 802 Main clubs description icon that means it is fetching what it is actually fetching this specific data from here because I have mentioned whether it has been it is pulling the entire list of data so anywhere you find this kind of a square bracket remember it is a list and inside a list there is one data here it could be this is one set of data in this one set of data if I go back to the notepad you can see it is there within the curly braces you see from this to this because it is one set of data now it might be possible while you are working on Json you could see there is a square bracket and within the square bracket you might find you know some set more set of data like this within the curly braces so this is one set of data because it is one set of data the first set of data this will have an index number so I am saying I'll put a index number here called zero getting it now in the previous output how the output came because I mentioned whether it also gave me the square bracket and everything inside the square bracket now I am saying 0 so let's see what is the output what is the difference in the output okay so I'll say Bengaluru [Music] hit on enter now look at the output now if I copy paste this specific data to the notepad now you compare by writing 0 what is the difference happened by writing 0 what happened it only pulled me the first set of data so I said because it only has one set of data it pulled from square bracket to square bracket when you mentioned whether it could have if there are multiple datas then all the data would have included within the square bracket now here I am targeting hey don't give me the entire data give me the very first data which stores in the index number zero so the moment I have mentioned in my code 0 what happened it only gave me from the curly braces till the curling places the very first set of data getting it now let's say I want to Target description now to Target description again it is pretty easy right only thing you need to remember you have to use the variable and the set of elements that are there in a sequence order so now I am going to say description again it has to be kept inside the single quarter double quote up to your choice okay so now if I'm going to run this and let me bring now if you see what happened the last output this line this code right this code gave me what output scattered clouds scattered clouds getting it now you should understand the very first output when we wrote whether this understanding is important when you have to fetch data from magician so when we wrote whether it gave me square bracket to square bracket data when I wrote 0 it gave me within the curly braces slide within this curly braces it has given me the data fine the moment I have written description then what is the output I have got scattered clouds what I have got is scattered clouds so getting it now anywhere you would like to Target all you have to do is simply write something like this so this is one thing okay because it had a square bracket you had to write the index number of that element which is zeroth element because this is only the first element okay so any indexes are will start from number zero don't start with the one it will always start with index number zero e if there are square brackets remember only if there are square brackets you are using indexes now let's look at the next condition I would like to pull the temperature now the temperature is there where temperature is there under Main either a square bracket no there is no square bracket there is only curly braces now if there are curly braces it becomes quite easy again for example I would like to print the temperature okay so let's see so I'm going to remove this print D okay Let It Be There so I would like to print temperature I'll create a variable called t then the entire data is there in the Json data variable so I'll use Json underscore data this is a variable next it is there inside the main this temperature is there inside the main so I'll Target that but first element of that area so here it says main so I'll put Main now if I print main what happens let's see you know slowly let's learn now if I print P let's see what is going to happen so let's run it again and let me print let me do it for let's go once again now what happened the moment I printed uh the broken clouds you understood right this is coming from here now this one for the temperature let me select this till the end and copy to a notepad this is what the temperature we are doing okay this is for a separate way let me paste it now what happened it is pulling the entire data the moment I said pull main so main means what main with this one right so that's where we needed the pretty data so that we can every time we verify it so if you see the main data here it is pulling from here to here because it contains only one element so it is pulling from here to here do you see just pulling here to here if I compared to the notepad you can see that ground level 996 starting with temperature 28.56 okay so it is moving now I have to Target the temperature now to Target the temperature simply write the square bracket the same process here we are not using any index numbers like previous because there we had a square bracket if you see here we had a square bracket for the weather and then we had a curly basis but here directly curly braces are there so you don't have to mention any index number simply I'm writing temperature now the moment I write temperature and run it let's give the same city name so now we can see 28.56 I have got so it says the city description is broken clouds and then the temperature is 20.56 now you have to learn a very important technique to print this how to do that it's quite simple simply say print and you know both the variables so I'm going to delete this print P and D because we wanted the sentence okay we want a sentence so I will say print and here I am going to say use the F string remember while you are while you have to print multiple various inside your sentence then format string we have to use okay for mastering and here I am saying the current weather description is okay I'm writing like this and within curly braces I can mention the uh variable D which contains my description okay and I'm going to say n so I'm writing the sentence just whenever I have to use a variable I'm using this curly braces so it makes quite simple okay and then and everything has to be there in this code okay within the code so I'll go back and say space n temperature is I'm going to put something like this temperature is curly braces and simply write t okay ensure your variables are proper case okay so I'm using capital K so I'm using Capital here so all done now look at this the final output how it's going to come so let me also remove this print print I don't need it now if I run it and finally let's see how the output is coming you can see the required output the current weather description is let me expand it the current weather description is broken clouds and temperature is 20.56 getting it so this is how you can easily fetch or extract a specific data from your entire API or from your entire Json so thank you guys for watching we will come up with more such interesting contents which are going to really help you build knowledge around the real world scenarios so please do subscribe to the channel and do comment in every video once you are done with the video Simply say done or write some comment of your choice all right so let's meet in our next content once again till then take care bye bye so thank you guys for watching your support is very much required please do subscribe to the channel and leave a comment on every video that you are watching simply say done give it a like and do share with your friends who would like to learn from our Channel thank you again you have a great day [Music]
Info
Channel: Automate with Rakesh
Views: 9,427
Rating: undefined out of 5
Keywords: Python json parsing, Python json parsing example, python parse json file, python json parser example, python parse json array, python parse nested json, string to json python
Id: nKtyWfqNmr8
Channel Id: undefined
Length: 14min 27sec (867 seconds)
Published: Sun Aug 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.