▶️ Watch on 3Speak --- Learn Python Basics Together 🚀 | Day 9 Welcome back to our Python learning series! In today's lesson, we explore JSON Handling in Python, an essential skill for working with...
Transcript
Hello, welcome to my YouTube channel, and today we are going to talk about JSON, handling JSON, right? So far we have covered so many things, and out of which the most recent one was handling files. And if you haven't checked all the videos, I request you to go through the list of videos which I have linked, which I have added in the description below. So let's get started. I'm going to create a new file, file number 18, and gonna... name it jasonhandling.py. So first thing first, in order to handle the jason, we need to import the package jason, and with that we'd be able to do a lot of stuff with the jason. So let's say I have a JSON data in a string. So I'm going to write jason data, or you can give any variable name you want, and in that I'm going to start writing values. in a string format or we can call it a raw JSON data which is not yet parsed. So how do we parse this JSON data? So I'm going to say JSON or say parsed JSON equals JSON dot and loads. This is the function that we have to use. So you can see over here it takes a string or bytes. So I'm going to use loads and in that I'm going to supply the JSON data over here. This function is going to take care of parsing the JSON and whatever it is, like, you know, it can be a list, it can be a dictionary, or it can be anything else, so it's going to parse it and we'll have that value over here. So let's try printing the name. So I'm going to say print function first of all, and let's say I want to print name. So in this example, we have dictionary, so let's print value for the name. Ah, I'm so happy to choose to adding semicolon at the end. right click and then run Python file in terminal. Okay, so it didn't work over here as expected. So after running this, we are seeing number of errors. So as far as I understand, there's some issue with the string that we have supplied. So I believe I forgot to apply the single codes for the keys over here, and because of that, it is complaining. Now I believe this is valid JSON. Let's try running this. Okay, we're still getting error. So what could be the error? Let's think about it. So I believe the error is not related to the JSON that we have represented. It's with the expectation. Expectation is to have the JSON data in single code. So I'm going to find all the single codes and then going to replace it with the double quotes. That's number one. And number two, we will start the JSON with single code and end with the double code. because double quote is the valid JSON key value pair structure in JSON. So I guess that's the mistake I did, but you shouldn't do. But in case if you do, you would remember that for the lifetime, because you would never do that mistake again. So let's try running this now. And now if I run this, in the terminal you can see Saga as an output. We can also try printing the employee ID. Let's try running this one more time. And in the new line, we can see the 700. So this is how we basically parse the JSON. What else? So now, imagine that I have JSON data that I want to convert it into JSON string. So this is like string and from the string we want to convert to JSON, that's we have done. Now let's say we have data in Python, and from the Python data we want to convert a JSON string. So let's create two object to JSON for now. I'm going to create a dictionary. created a dictionary over here and we have all those things right over there. So, now I'm going to declare another variable to JSON string equals JSON. So the way we have loads function, just like that, we also have dumps function. Loads to convert or say parse the JSON, and then dumps to convert back to string. So in here, I'm going to supply to JSON. And now, in response over here, or say as a result of this, statement will get the string value let's try printing the jason to jason string i'm going to run this now and in the terminal you can see we have the jason string which represents the dictionary which we supplied so this is how you can convert from string and to string i hope that it helps you to understand how to deal with jason data data and that's it for this video see in the next one