JSON Concepts To Remember ๐
May 7, 2020 05:30 ยท 1999 words ยท 10 minute read
hey YouTube this is Prince from Desi programmer and welcome back to another exciting video and in this video we are going to talk about JSON also I took our almost everything about Jason in this single video and to get the most out of this video although you don’t need to be a pro Gorder but still you should have some programming experience I’m actually planning to roll out a couple of videos about web api’s even in node GS and infra so I thought it would be better to first give an insight about G Sean now talking about Jay Sean which stands for JavaScript object notation it is a lightweight data interchange format which is easy for us that is humans to read and write and also is easy for machines to parse and generate jason has almost replaced XML from the market and there are other alternatives from Jason - like ml Jason ain’t a programming language first of all it’s just a text storing form it you can use JSON for almost all the modern web api you can see it being used in almost all of them and also it’s used widely for destroying configuration data such as our vs code settings itself now in this video I’m going to use node.js which is essentially JavaScript and also Python - posh and work with JSON data so just as you can see I have an indexed or GS file which is JavaScript I have a data digestion which contains some JSON data and I have Python file where we are going to write our code regarding such things but before you proceed let me clarify one thing that Jason ain’t exactly a text file format it is more like a data representation format in simple words if I say then we don’t always need a JSON file to work with JSON data anyway let’s just escape all this talk now let’s take a look at the code so talking about JSON and let’s just not pay attention to this data that we have right now although JSON ain’t a programming language but there are certain kind of data that jason kept process so if you are going to insert some data in your JSON file then it should either be a string such as this or a number or a JSON object such as this or an array it can even be a boolean like and it can even be a null value but other than this you can’t put any other data in your JSON files not to work with JSON using any programming language or a text stack all that you need to know is how to parse JSON in that programming language that is how to convert this data into a native data type of that programming language for example I have this imported of JSON file let me just copy this object and let me just delete all of this and now this is a simple JSON object it contains some key value pair user ID ID title and completed now all that I have to do is to parse the JSON data in my indexed or GS file now since gesture itself stands for JavaScript object notation it’s actually very easy to parse the JSON and use it with JavaScript with node.js if you want to parse JSON all that you have to do is to assign some variable and then even when you require that JSON file it’s going to be automatically parsed so just as I required this if I try to log JSON data and now here in my terminal all that I have to say is node space industrious and if I save this if I run this again you can see this gives me some JSON data things become interesting when I try to show you the type of this data and this is an object now this is the point where you have to pay some attention now the object is something that’s a need you did a type of JavaScript while Jason this too is a JSON object but here it’s a native JavaScript object so now if I get if I have to get any data out of this I can just interpret I can just look at it like a simple GS object so now if I want to get the value of completed that I have to say is JSON data dot completed say wait run it again and here we have false easy-peasy now to use the same gesture in Python all that we have to do is we have to import this JSON module that comes with Python by default then we have to say with open our file path as f we load we use the json dot load method to load the data from the file which will essentially parse it and then we are saving it in data variable and i will just print the data let’s save it let’s go back to our terminal and let’s run this now you can see that we have this simple data which is in silly our JSON data but thing will become more interesting if I just try to print the type of this data and now it’s going to be a dictionary so here is the thing Jason is an object like right here this JSON data is an object but you don’t have to worry about that all that you have to worry about is to parse the JSON so that you’ll be converted in a native data type of your programming language which is dictionary in Python and object in JavaScript so now here if I have to let’s say print the title of this task all that I have to say is data dot title but it’s not going to work why because this is not how you interpret dictionaries in Python although to work with objects in Java in JavaScript we use this same simple syntax so what most people think is that it’s same parsing JSON in all the programming languages but it’s actually not in Python all that I have to say is data and then we have to index the title using this method and now if we run this you can see we get the same title we get the same task as we have let’s go back here and let’s actually grab a couple of more tasks and now this is going to be a JSON array so we simply have an array in that array we are going to have multiple JSON objects so now I have three tasks if I again run the same code in my JavaScript I’m going to get undefined that’s okay Farren here I’m going to get some error Y now we have an array so in my indexed or GS I don’t have to say generator not complete if I just say yes on data I’ll get the entire data if I say the type of data now see things are going to get interesting right here it’s not going to be an array it’s still an object because in JavaScript we can even look at this as an object we can manipulate it and we can access the data but let’s go to Python and now here I have loaded this JSON data in my data and now if I just print that type of this data then this is going to be an array it’s not going to be a list so just as I said when you parse JSON it gets converted into a native data type of that particular particular programming language and all the data is stored in that particular variable with that data type so now that we know it’s a list if I have to access let’s say ii ids title or that i have to do is that i have to say first go to the second index which is sincerely first because it’s going to be 0 and 1 and now in the first you know in the in the element at the first place we need the title see here i have to say in the first place element i need the title let’s save it let’s read again and here we have the output which is essentially the same as this one in in the index to Egeus if i have to say something like that all that i have to do is that out to say d shanita go to first index and here i need the title and this will again give me the same output as this one so is in city same as this but again just as i said we don’t always need a JSON file to store JSON data we can even in stores you should attend simple variables and work with that in JavaScript let’s have distortion we shown yet I can say this war data is equal to and this can simply be like this like if I have to store this single particular object I can just store it as it is in JavaScript and it’s not going to give any row which is why mostly we say that it’s easy to interpretation with JavaScript and maybe this is the reason why node.js is a very popular framework for API development but you have to do the same with Python I can’t do it in that way because python is going to give me errors so in Python let’s say if I have a data which is going to contain some JSON so I can say that in this string now this is going to be addition I’m going to have some name which again is going to give issues because Jason can’t contain single string so let’s say that our Python is going to have them in single string I have a name it’s going to be Prince and I’m putting the ID value to four I just save it let’s just comment this out and let’s just print the data now here when you print this it’s going to be a simple string let me just show you the type of the data so now it’s going to be a single string so what this means is that here I can say something like data and then if I try to index the name we’re actually going to get error because obviously we can’t slice our strings so to do that all that we have to do is that we have to again parse this data in our Python which is not the case in JavaScript because Jason fits pretty well with JavaScript anyway in Python I have to pass this so I can say some JSON data is equal to json dot load and how to load this data right here and now if I try to print JSON data we just save it let’s just run it okay we are going to get error it should be loads and now we are going to get this value now this is a JSON data if I try to get the name I can simply get the name which is prints so I think that’s pretty much it from Iceland in this video all that I heart to speak in this video is that JSON is just simple text representation format you don’t always need a file to store JSON and if you have to work with Jason in any programming language all that you have to find out it’s a way to parse JSON in that programming language I won’t spend too much time talking about encoding and decoding data because that’s what we are going to cover in our not API tutorials but till then keep coding keep loving keep sailing and peace .