hello guys and welcome to a new video
from hacking oils
in this video we will learn how to
extract data from a json file in python
technically json also known as
javascript object notation is a very
popular data standard used for
exchanging information and for also
representing structured data it has
become a completed norm to date
it has become a common norm to transfer
data that is used in apis in the form of
a json
and this is exchanged between the client
and the server side for web applications
in this video we will get ourselves
familiarized with the json model present
and also some important functions that
you can use in your programs so without
further wasting time let's begin the
video
[Music]
so first thing what i've done is i've
imported the json module that is present
and you do not need to install it using
pip or something it's already present as
a default module as soon as you install
python
and then i have created a simple python
dictionary and a json object is like a
python dictionary which it has a set of
keys
and values however it is not a python
dictionary since it's a string so
basically you write a python dictionary
and then at the start and end you put
the parent thesis
denoting that it's a string
so now
this is a representation of a string in
python however when we convert it to
json object it will be a representation
of json format
to convert a python string to a json
object you simply need to use the dumps
so i'll create a variable called as stem
file is equal to
json and then i'll use this function
called as dumps and inside the dumps i
will put the data that i am working with
that is the data variable stored over
here
and now i'll use my context manager and
say with open
and now i'll create a file name so i'll
just call it my
file
and its extension will be a json type
format so i'll say myfile.json
then comma i'll put an argument of w
which means i am writing data into that
file
and then as file
and what i'll say is now
file
dot write that is the write function
present inside the python file handling
method
and inside that i will write the temp
file that i have created
so basically we are opening a file
called as myfile.json
and then
we are writing the data stored inside
the temp file variable
and now if i run this program
okay i just wrote the spelling of json
wrong so let me make this current
changes and now if i run the program as
you can see it says finished which means
the program has compiled successfully
and now if i go inside my folder as you
can see a file called myfile.json is
visible and i click on that and now you
can see your data present inside a json
file
now the next thing that you would like
to do is
obtain data from a json file so you can
even do that and for that i will again
use my context manager so what i will
say with open
my file
the file that we were currently working
with
dot json
and this time i will place in a argument
of r since we are reading from it and
i'll say as a file
and then i'll create a variable called
stem and then i will say json
dot load
and i'll put in the data that i have
saved in my context manager that is file
and now i'll simply print in the temp
variable so i'll just print them and if
i run the program as you can see
the data that was stored inside the json
file is now present inside a python
program
now i would like to talk about the few
functions that are present inside the
dump s module if you treat the data that
we are using
as a dictionary instead of a string so
i'll just remove these quotation marks
and now if i use the dump s module so
i'll say temp is equal to
json
dot dump s
and then i'll put in the data as an
argument then we have a function
argument called as indent
and if i put the indent to 3
you will be able to see certain
difference so let me show you that
so i'll just show you printem
and as you can see now the output is not
visible on a single line but it is
indent in a perfect manner since the
argument indent was given inside the
function
another important point that we need to
remember is
the data representation of a python
object
with its json equivalent data type
so dictionary is stored inside a form of
an object in json
then the list and tuple that we have is
stored as an array
the string is also stored as a string
variable and any type of number such as
an integer float or decimal is stored as
a simple number in json data
and the data type none
is stored as null also known as null so
it's important that you know the
equivalent data stored inside json while
working with your json data so that's it
from this quick video where we
understood what is json and how to
handle and work with json data if you
like this video do drop a like and
subscribe to the hacking's youtube
channel and press the bell icon for more
fun and amazing videos
you