Friends, here we are going to work with the Json files using Python.
What is Json?
You know Json is a JavaScript Object Notation,
and it is a popular data format used for representing structured data.
[no audio]
Guys before Json, you know XML, right?
What is the purpose of XML?
The purpose of XML is just to transfer your data from server to
application, or from application to server, or simply server to client or
client to server. The same way
we are using even Json as well.
[no audio]
Right?
See let me show you a simple Json file.
[no audio]
So, we have a simple Json file, let me open it.
Yeah, see here.
This entire thing is one Json file and if you observe
in that you have curly brace starting and ending. So in terms of Python,
this is a dictionary, but as per your Json, this is a object.
You should say your dictionary as object in your Json.
Fine. See, for the same Json file, whatever the content we represented,
I mean whatever they represented using this Json format, same data
they're also going to represent using XML.
That means you can write your data to transmit over the network
either by using XML or by using Json. For simpler data
we are going to use Json format, for complexity data
we are going to use XML file.
[no audio]
Now, let me copy this simple Json file and I want to store it as some,
with some Json file, let's say 'myjson'
[no audio]
'.json' extension.
So, this is now your Json file.
So, what we are doing here is we want to read the Json file or
if we want to write the content to your Json file how we can do it?
First, let me go with how to read a Json file.
So I am going to store, I'm going to save program or script name as
working with the, yeah working with Json files.
[no audio]
Right? See guys, whenever you want to work with the Json, first
of all you need to import a Json module, which is a default module then
let me take 'file', or 'req_file',
what is the file you want to read?
So guys, you know, it is a better practice to provide complete path,
so as of now directly I'm giving 'myjson.json' file name itself
without giving complete path because that Json file
and my script both are there in the same location.
But anyway, for better practice, please, please provide complete
path for your file whenever you are writing Python scripts.
That's fine. Now guys, generally if we want to read a file, if you want to
read the content of a file what are our steps? Open that file
in 'read' mode, that is the first step. Open that file in 'read' mode.
You know even if you don't mention any mode in open
function, by default that is the 'read' mode. But if you want to mention,
yes you can mention it, 'read' mode.
Then finally, we are closing. Between these two
whatever the action you want to perform
you can perform. See this is not a normal text file.
If it is like a normal text file directly we can print 'fo.read'. If it is normal
text file we can use this one, right? You are getting
but you are getting like, again this
as a Json only. But what we need? We want to work with the Json file,
that means you need to convert that data into Python
understandable data. Now, again, this is like a string.
This is just your Json object as it is.
But, we don't want that Json object because if you want to modify
if you want to get, suppose, let's say based on keys,
if it is dictionary based on this key you can get this value,
but now this is not a dictionary, then you can't to do some
process on this data like, you know key-value operations
on your data because that is not a dictionary.
What happened? One, two, one more parentheses.
Yeah. See, that is a string. You're able to read your data, your Json data,
but that is a string itself. But suppose for seeing
purpose how you are getting that data? As a dictionary, but that is not
a dictionary. How you can process that? We can't process that.
So now, what I am doing is I am going to convert this into your Python data.
So, for that what you have to do is simply your data,
directly you can print it. 'print', use 'json.load'
You have to load from your Json. Simply if you parse your content
your file object to your Json object, Json module
Json will convert your data into Python understandable data,
that is simply dictionary format. Now see the result. Now from
this data I can take simply 'get', because there is a dictionary,
right, I can do some operation called, '.get'. Now see the result.
Yes, you are getting something like that.
Now I can do one more operation because this is also dictionary but
previously with 'fo.read' you are not getting your data as a
dictionary, you're getting something like a string.
So, on that string you cannot perform your dictionary
operations like key value operations.
That's why we are converting the Json data in terms of dictionary. For that
we have a simple syntax called, 'json.load', your file object.
That's it. Very simple.
[no audio]
Right? See, if you want to print your Json data as it is you can also do this one.
[no audio]
but don't execute those sixth and seventh lines at a time.
You have to execute anyone of that,
because once if you read the content you don't have anything to
read again in that.
[no audio]
Right? Okay, fine.
[no audio]
Now this is simple way just to read your data. Now what I want to do is in case
in my hand if I have some data how can I store that data into
a Json file? So before going to store into your Json file
you need to think about what type of data you have in your
hand. You know while reading the data from your Json what
is the format you are getting? Dictionary format, that's why
whenever if you want to store your data into a dictionary,
into a Json file you should have in your hand data as a dictionary, right?
See let me do one thing. I'm going to create a 'my_dictionary'.
Suppose I am going to create dictionary like "Name",
I am writing as "Narendra".
Then I am writing "skills",
[no audio]
'Python', 'shell', 'yaml', 'AWS', something like that.
Right. Now in your hand you have a dictionary data. This dictionary data
you can able to write into your Json if you know the syntax.
So now let me try for syntax.
So first of all, just assume that you are going to create a Json file
Let me take 'req_file' as, suppose 'myinfo.csv'. Sorry, 'json'.
So, this is the Json file I want to create with the help of our Python.
So, what I want to write in Json file? I need to write this data.
What is your data? It should be a dictionary.
Right? Then fine.
Let me open first your file in 'write' mode. Whether it is Json or csv or txt file,
the first step you have to do you have to open it in your required mode.
I want to write the content to your file
Then I need to open it in 'write' mode. Then finally just to close it
between this you need to write your data into your
Json, right? How we can write our data into Json?
Right? Fine. See that, what I am doing.
[no audio]
Simply, 'json.dump'.
Just we are pushing our data into Json file, 'json.dump',
your dictionary data
[no audio]
with respect to your file object. That's it.
Now, let me run it and see the output.
So you just pushed your data into your Json file.
Now, we don't have any output here, right?
We just created a file. Again, if you want to see the data
what is there in your dictionary
you need to read the data, you need to read that Json file.
So, just now we created a Json file called 'myinfo.json', let me open that file.
[no audio]
'myinfo' or 'myjson', what is that?
I think 'myinfo', right? yeah. See that.
[no audio]
You're getting again a dictionary in a single line as of now.
Of course, this is in a file.
Now, you have to say that this is a Json data or simply object in your
Json. But whenever if you see, here right, Json data
consists of some spaces, some indentations,
right? But that type of indentations you are not getting
here while creating your Json file.
It's simple you can create it.
Nothing is there. While writing your content into your Json file you need to mention
what is your indent. So I want to take four spaces.
Just run it and open your file, Json file
once again. Now you will get indentations clearly in your
Json file. See that. Now you're getting clearly, right?
That's it.
[no audio]
So, guys, this is the simple way to read content from your Json
and to write content into your Json.
The only thing you have to remember is reading is nothing but
loading content from your Json. Writing is nothing but
whatever the content you have that we are writing into your Json
or pushing into your Json or dumping into your Json file.
Finally, whenever if you are reading the content from your Json you are
getting your output as a dictionary with the help of your 'json.loads',
sorry, 'json.load', and whenever if you are trying to dump your data
into a Json file, your data should be again a dictionary. That's it.
Right? Fine. See here.
Finally, we have small diagram here. In terms of Python if it is a
dictionary, in terms of Json
it is an object. So whatever the Python lists and tuples are there,
they are simply Json array, string is string only. Integer, float and whatever
it may be complex, they are numbers in your Json.
So true, false, see that, true and false with small letters, none, null.
So, if you remember this while working with your real-time,
you know, those who are going to work with your AWS side
they're going to create so many times policies.
So policies are in terms of Json format, right? So there it will be very helpful.
Okay. So just remember this so that you can easily understand how we can convert our
data into Json, or from Jason to Python. Right? The main thing
you need to remember this.
Your Json into, your Json data into Python is always a dictionary.
If you want to write your content into your Json, first thing, you
should write your data in Python as a dictionary that
data we need to dump into Json, then that will become a object,
That's it.
Simple, right?
Okay guys, thank you for watching this video.
[no audio]