0:00
hello guys welcome back to the channel john here so today we're going to learn how we can work JSON data in Python but
0:05
before that okay let us define what is JSON so JSON stay for JavaScript object notation okay we wrote this way okay
0:11
JSON so this GS here the first GS okay it stay for JavaScript and O stay for
0:17
object and N stay for notation okay so it is a lightweight and widely used data
0:22
format for storing data and exchange information so despite his name being
0:27
like JSON okay and the JavaScript being here okay in the first JSON it is
0:33
independent and supported by many programming language okay including Python also so in this video we will try
0:39
to know better we will explore how we can work with JSON data in Python from the basics itself okay and we also going
0:46
to have some kind of a practical experience by accessing data from any API okay any free API so but as I said
0:53
okay we will learn how we can work with JSON data like writing the JSON data how
0:58
we can load the JSON data how we can convert the JSON data into Python object so and many things on okay so but before
1:06
we start okay if you didn't subscribe to this channel please subscribe to the channel and give a like to this video so let's start JSON structures data using
1:13
key value pairs and order lists it is similar to the Python dictionaries okay and list also so here is one example of
1:19
JSON data so this is a simple structure of a JSON data okay as I said okay it
1:25
looks like additional and listes in Python so we have this pair of okay open brackets and here inside we have some
1:31
kind of list okay so this is the object that we will work on for example here people represent object so this object
1:38
people it has a lot of datas inside okay or a lot of uh lists of dictionary okay
1:43
for we have the first information the first data which is this one and it is separ separated by comma which is this
1:49
one okay and the second data is this one okay which is also separate okay the this comma separates the both of the
1:56
data okay so we will learn how we can work with this thing okay with this type of extractor data and the first
2:04
operation that we are going to perform here will be converting okay between JSON and Python object so in order to do
2:11
that okay we need to use JSON for that okay so how we are going to do this
2:16
let's start so in order for us to convert this uh data okay this JSON data into Python object okay so we can use
2:23
the built-in model which is JSON but from Python okay it allows easy conversion between JSON strings and
2:29
Python object so how we can do that okay to do that we need to do this okay so this is the information that we are
2:34
going to convert into Python object so for that I'm going here in the top okay let me give some space so first we need
2:39
to import JSON inside of our script okay I'm going to do something like this okay import JSON
2:46
import JSON so this way we import JSON into our script so to in order to
2:52
convert this thing okay first we will do something like this i'm going to create
2:57
any variable I will call it as a JSON JSON data json data will be equal okay so
3:04
this JSON data will be inside of uh uh quotes which will be okay one two
3:11
three this way okay i'm going to open here and I'm going to copy all of this thing inside of that quotes okay I'm
3:18
coming here let me give a little space and boom i will do something like this
3:24
so this way I just put the all this information okay inside of this variable also like this if I'm going if I print
3:30
this thing I'm going to get something like this okay let me see print uh print what i'm going to print JSON JSON data
3:38
which is this one save and if I run this script let me show you as you can see it
3:45
just print all this extractor okay so this is not yet converted into Python
3:51
object okay so for us to convert this extractor okay into Python object we need to do something like this okay come
3:57
down uh I'm going to come here give a little space so I'm going to call create another variable called Python data okay
4:04
Python data so this Python data it will be equal so now we we are going to make use of JSON itself json dot loads this
4:13
way here dot loads uh open and close what I'm going to load I'm going to load
4:18
this information that I have here okay which is JSON data so I'm going to come here JSON
4:24
data okay JSON data boom so now this way if I print this Python data okay come
4:32
here let me print it put here if I save and if I print it let me push this
4:41
up now if I print this thing okay let's print now first let me clear here so
4:46
that we can see the things
4:52
properly let me clear this clear now if I print this thing okay let
4:58
me run the script boom as you can see now is is printing a Python object okay as we can okay people then here we have
5:05
a peoples as a key itself okay then we have a list of dictionaries okay so this
5:10
is the first dictionary which start from here and stops here then we have the comma and this is the second dictionary
5:16
so this is the way that you can convert any structured data JSON data okay into
5:22
Python object so now the next operation that we are going to perform will be create read and modify a JSON file okay
5:29
so now we are going to work with JSON file so for this okay we are going to need something like this okay initial
5:34
okay so we'll be using this data and don't worry about this okay I'll keep it in the description of this video so that
5:40
you can make use of it okay so this is for me to don't type okay because if I type this might take a long time so
5:46
that's why I'm just using like this okay I'll copy this the same information okay in the description of this video so the
5:55
first operation that we are going to do is to create a JSON file by using Python
6:00
okay so in order to create this okay as always first we need to import JSON here in our script okay let me call import
6:07
JSON this way as always okay save so after importing JSON okay we have this
6:12
information here okay in order for us to create any JSON file by uh using Python
6:19
it will be something like this see first I'm going to open this okay
6:26
I'm going to open any file okay I'm going to open the file with Python okay
6:31
so it's like that if the file that I'm going to open exist it will open okay if
6:36
it doesn't exist it will create that file then it will open it so how I'm going to do this it's it will be
6:42
something like this see with okay with open with open what so I'm going to give
6:49
the name okay so this is okay this information is about the states okay the states of Brazil okay so for them S
6:56
paulo is the one state of Brazil rio de Janeiro is one state of Brazil minajer Bay and Parano okay those are the
7:03
information of states of Brazil okay so this is the abbreviation of the state of SAL which which is SP and this is the
7:09
area code which is 11 okay so and the same code with Rio Janeiro Miner Bay and
7:14
Pana so I'm going to create uh I'm going to call this JSON file that I'm create as a states okay so I'll call it states
7:22
dot JSON this way okay states.json json so since I'm opening this okay and
7:29
since I want to create a new uh JSON file okay I'm going to open this in
7:36
writing mode okay so for writing mode I need to give which is for writing mode
7:41
i'm going to give comma and after that okay I'm going to use encoding also encoding this way okay so it's very
7:47
important for you to give encoding okay because if you don't give encode it might get error while open the file so
7:53
in order for the things to open properly is very useful is very necessary for you
7:58
to use encoding so that's why we are going to use encoding UT uh UT what
8:04
UTF- and 8 then we are going to consider all this thing as F okay then the dot
8:12
I'm going to come the next line so after this okay I'm going to use
8:17
JSON dump okay so JSON dump is the one that help us to open a JSON a JSON file
8:24
okay in Python so in order for me to do that I'm going to do this okay JSON okay
8:30
JSON dot dump this way okay JSON.dump i'm going to open so the first thing
8:36
that I'm going to give is the information says which is the data okay so the data is this one okay the data
8:42
initial so I'm going to come here down okay I'm going to paste data initial which is this guy okay
8:49
comma then I'm going to give the file that I'm going to open or to create which is this state okay which will be
8:55
this f F comma and then I'm going to use something which is really very important
9:01
okay that help us to keep the JSON file well formatted okay a very indented so
9:06
which is this one I indent okay which is this indent i'm going to give two okay
9:12
so that it will format the file very well okay like giving two space so after that okay there is another param that we
9:19
are very important for us to give which is this one okay insure ask so in should ask key is for okay for them there are
9:24
some words okay which has some kind of a special character something like this for them if you check is them s okay s p
9:31
it has something here in the top of a that in Portuguese we call it as a title okay so this is kind of a special
9:36
characters character so in order for us to open or to create or to save this information properly okay without any
9:43
damage it's very important for us to use to give this parameter insure as key okay so that when you give that okay it
9:49
will open the things or it will save the properly without any damage of the information okay so this also goes for
9:56
special character like for examp if we have like a robot if we have some any kind of a special characters okay in
10:02
alphabet is very important for us to give this insure ask then we just need to give like fails something like this
10:09
okay fails so after that okay this way I'm just I'm creating the I'm writing
10:15
into the JSON file okay I'm creating JSON file so now the next thing that I'm need to do is to print so that we can
10:21
see the result okay if it really work it or not So for that I'm going to do something like this okay print open what
10:27
I'm going to open okay let me do this boom uh I'm going to up okay file file
10:32
okay file okay this this is just a name that I'm just going I'm giving okay so
10:38
that it can we can see the Okay file states
10:43
state.json states.json okay oh sorry state.json's created okay let's say
10:50
created create created okay so this is for just so that we can get any
10:57
information that okay the file was created okay so after that let me give one kind of slash n so that it can format well my input okay so my output
11:05
better okay let me clear this clear clean clear clean uh-oh it's clear not
11:12
clean i mistake okay now I have to waited for process okay exactly is clear not clean clear okay let me clear this
11:19
now let me run this uh piece of code so that we can see what the result if I run this as you can see it print me file
11:25
state.json created as we can see now we have here one file called states.json okay if I'm coming if I
11:32
click there and as you can see it just created this file okay as you can see
11:37
states okay then we have name s Paulo abbreviation of s area code 11 name and
11:43
so on so on so on okay so it just create our JSON file so it works like that okay
11:48
if uh the file exist it will like write inside of that file okay if it doesn't
11:54
exist it will create the file then it will post it will save the information there so those information we just save
12:01
as a JSON file as you can see even the icon okay is kind completely different okay it's kind of like open bracket
12:07
bracket open bracket and close bracket that is how JSON file are identified so the next operation that we are going to
12:13
perform will be how we can read and assess data okay JSON data in Python so
12:20
for that we are going to do something like this see in order for us to read okay we need to do something like this
12:26
i'm just going to comment this uh line of code okay I'm going to comment this
12:32
okay even this one okay let me comment them yeah they stay together boom so
12:37
okay in order for us to open to read and assess the data we need to do something
12:43
like this okay I'm going to use this almost the same thing okay with open let me do this so what we going to
12:49
assess we need to assess this this uh data so that we can do some manipulation okay let me assess states okay is
12:57
states statejson state.json JSON and since I'm
13:03
open it for reading okay so it means I'm going to give the read parameter okay
13:08
which is R this way okay R allows us to read the information so after that okay
13:15
let me use also encoding I never forget encoding okay it's very really really important to use encoding encoding what
13:22
I'm going to give as always okay UTF plus and 8 so if all this thing
13:29
let's consider as f okay dot so we are
13:34
reading the information okay so for read information I'm going to create a variable called data okay so this data
13:40
will be the the information that I'll be manipulating so data so let's use now
13:45
JSON dot okay to open we need to just use the load function okay which stays
13:50
like this save I did a mistake here okay is JSON
13:57
JSON JSONload I forget give the F okay which is the file JSON.load lot f save
14:06
perfect so now we have the data here okay so we have this data uh which is which contains all this information here
14:12
now let me close this okay close that one so come here perfect so now uh the
14:18
first operation that we are going to do okay uh let's print the states okay the information so let's assess the data and
14:24
print it so in order that let me print okay print what I'm going to print okay i'm going to print the states okay let
14:31
me see states okay status and uh abbreviation abbreviations
14:39
abre abbreviation one B is missing with abbreviation abbreviations state and
14:44
abbreviations okay dot oh sorry dot and save
14:50
so and I'm going to some kind of final meaning blah blah no that's not important okay states and abbreviation
14:56
okay come here so after that okay I'm going to do something okay for us to assess the the
15:04
information which is inside of this data I'm going to use the for loop okay for states okay so states now let's use
15:12
state only for states in in what in data which is this information okay for
15:17
states in data and let's use this okay so we just need to assess the states and
15:23
the abbreviation okay so if I come here into this state uh this JSON information so okay I'm going to assess this stage
15:30
which is the object Okay because this object state has all this information okay let let me assess status come here
15:37
okay I'm going to give here like this okay status okay for state in status I'm
15:44
going to do this dot what I need i need to print open okay I need to I'm going to
15:51
use F string okay F let me use F string then I'm going to print okay let me do
15:57
this okay state i'm going I'm using this state okay state h what I need to assess I just
16:03
need to assess okay since we are here on when I get data states okay which means
16:09
I I already got all this information okay so inside of this information now I need only the name the name of the state
16:17
okay I'm going to use the name state name okay state name
16:24
and the abbreviation also okay kamir give I'm going to open bracket parent
16:31
this way okay then let me get the state okay
16:37
state i need the abbreviations also okay what is the abbreviation h
16:42
abbreviations so this is how you can assess or you can manipulate the JSON data in Python okay I'm going to use the
16:48
abbreviation save now if I run this thing okay let's run this a little down
16:55
boom if I run this guy let's run this save and run boom as you can see we got
17:03
the states S paulo what's the abbrevation of S Paul sp if we come here and check as you can see uh S paulo
17:09
what's the abbrevation of S Paulo sp okay and we got for all the states okay s paulo Janeiro Mina Rise B and Pana and
17:16
so on so this is how we can read and access the data in JSON file list by
17:23
using Python so the next operation which is also very really really really good important uh information operation is
17:31
removing area okay removing data inside of a JSON information uh data so in
17:36
order to do that okay we are going to do something like this see I'm going to come here okay let's use again for okay
17:44
for state in in what in data uh data what let's assess only states
17:52
okay states let's do and dot okay like this boom perfect so uh we want to
17:59
remove the the operation that we're going to use here is to remove the area code okay
18:05
we want to remove this this one okay this area code in this information okay so as we know okay is always good when
18:13
you are manipulating data okay never mess up with the original data okay because you might need this so when
18:20
you're doing uh manipulation of data okay always create a copy create a new data that will put this uh that will
18:27
copy from the original source and put in the sample source or something like that let's say like that so first we need to
18:35
check okay let's check if area code exist okay if area okay if area
18:42
code come here okay in state if area code in state okay let's do this lot dot
18:48
and come down so what we going to do is okay we are going to delete delete what delete state okay state
18:56
area code delete state area code perfect
19:01
save Okay so like that if I run this okay let me run boom uh it just it's
19:09
only printing it's printing almost the same thing okay because we didn't print it at all so uh this piece of code okay
19:16
it will remove the area code okay so what we can do so the next thing that we are going to do it will be something
19:23
like this see and let's reuse this guy okay this code that we have here okay
19:29
let's reuse them i'm just going to copy come here okay so after this
19:34
operation which is removing operation okay uh I'm going to paste these guys here come here okay delete this and
19:42
delete this so as I said okay never mess up with the original data so in order to do that let's create a new data okay
19:49
let's call it like a new states okay json let's open the same
19:55
way let's use encoding as always and here let's keep like this okay so what we are going
20:00
to to give it okay so we are not going to do with data initial okay which data
20:06
initial is this one okay so the one that we are going we are working on is this one is the data so let's give the data
20:13
remove this then let's give this one data and here let's keep the way that it is okay so now let's print the
20:20
information and let's say okay file states okay file uh new states file
20:29
New new states save it save
20:34
it save it without let's use without okay save it
20:40
with without save it without area without area
20:49
code save so for that I need to print it okay
20:55
so for me to print it uh it's very easy i just I can just reuse this one also
21:00
okay and no I can just reuse this one okay i'm going to copy this information
21:05
that piece of code come down and paste it okay so what I'm going to do for data
21:12
in uh no no I need to assess this one okay
21:17
so for that I need to grab all this too i need to get this
21:25
full come here okay and okay so because I need to open the new new information
21:32
okay so new information will be this one new states JSON come here okay instead of this let me give okay new state JSON
21:39
and let's open uh like this okay let's call it data then let's say for data into blah blah blah is something like
21:45
this okay so this is okay now let's come here okay and into this one okay let's this
21:54
one let's print with area code okay let me add also one more thing and just let
21:59
me come here i'm going to copy this one give a little space i'm going to give
22:05
like this paste and now I'm going just to give that area code okay so this one
22:10
it will print with area code and another one will print without area code okay save okay perfect now this one will
22:18
print without area code okay save and if I run this let me come it up if I run
22:23
the code let me see boom as you can see everything work okay so this is the first ones okay with area code S paulo
22:30
SP abbreviation 11 the area code then we have okay file a new state JSON service
22:36
without area code as you can see states and abbreviation S paulo SP something like that okay and
22:42
also it created a new file as you can see new states okay if I come in this new state and if we check that new state
22:49
so let me come this guy down if you say new state name s Paulo abbreviation SP
22:54
there is no area code okay but this old one has area code okay so guys this is
23:00
how you can manipulate JSON data by using Python okay so and the new
23:06
operation that we are going to perform it's not a okay so this is something we which is very good that we are going to
23:11
do so we are going to try to assess any information from any API which exchange
23:17
rate API and let's work with that thing and let's see because that is a very
23:22
really really good example okay it's a practical example okay with information from current exchange API integration
23:28
okay so let's do this thing and then we can close the our projects okay so in
23:34
order to do that let's do something like this okay I'm going to use this one let me clear this so and Let's uh consume a data from
23:43
the exchange rate API okay so in order for us to do that okay let's do
23:50
something like this uh I'm going to import JSON itself first as always okay import JSON import
23:57
JSON oh JSON import JSON oh let me give a little Okay this way is good you guys
24:03
can see properly import JSON so after import JSON uh what I'm going to do so the next
24:10
operation the next thing to do will be uh control B let me I think to be like this will be
24:15
better import JS okay the next operation that we need to do I'm going to use from
24:22
ur okay from url lib from url lib import what I'm
24:29
going to import uh url lib dot request dot request okay lot request import I'm
24:38
going to import URL open your URL open perfect so um the first thing that we
24:46
need to do is oh sorry then the first thing that we need to do the first thing that we need to do is to fetch data from
24:53
the API okay so in order to do that I'm going to create a variable called URL okay this URL will be equal so the URL
25:00
that we are going to use will be this one okay don't worry I'll keep it in the description of the video so we going to
25:05
use this one http https dot dot what and slash something
25:12
like this then say open er
25:19
okay okay okay underscore like this
25:27
v6 okay this is one end point that we can get the latest information okay
25:32
latest latest and underscore again USD okay we can get the latest uh
25:39
information of USD from this URL perfect so save now if I print this guy okay
25:45
print let's print what url perfect save and let me clear it
25:52
clear and if this guy come up okay save now let me run as you can see it's just
25:58
printing the URL itself okay so after that the next thing that we need to do is let's get the response okay response
26:06
response so in order for us to get the response let's use the URL open okay let's call URL open okay what we are
26:14
going to open we need to open the URL that we just gave there okay URL save now if we print this response okay let
26:21
me copy and paste here boom save and if we run this guy let's see what's going
26:28
to give us boom as you can see it gave us HTTP.client HTB response object we have something like this okay perfect so
26:35
this UR URL okay it give us also the information as a JSON file okay so in
26:41
order for us to open this information properly okay let's use the JSON uh JSON
26:46
itself okay that we imported so let's do this okay let's create a variable called data JSON data JSON so this data JSON
26:54
will be equal JSON do.loads open what let's open the
27:01
response that we got here okay response dot read this way okay open and
27:07
close then dot decode okay because we need to decode this information so that
27:12
we can see it properly okay decode open and close and do this UTF
27:18
okay underscore uh not that's not underscore okay hyphen 8 perfect okay
27:25
save so now if we print this data JSON copy come here okay and boom save now
27:32
let's clear this so that we can have more space clear if we do this save and if we run okay let's wait since because
27:38
it's going to assess the URL and it might take a little time because my internet is very slow okay and if you
27:44
guys also want to help the channel please you can donate to help me so that I can improve some stuffs here as you
27:50
can see it's give us this information okay as results result success provider
27:56
this one documentation so this is a JSON uh this is a Python object okay that it
28:03
give us gave us okay is getting from this JSON file something like that okay so the thing that we need to get is the
28:09
rates okay we need to get this one the rates okay rates it's here and this is the information of rate rates for USD
28:16
for uh Arabian USD for Afghanistan for all for AMD for NJ is my NJ I don't know
28:25
which country is this but this A O A is my country which is Angola and BZ
28:31
something like that a lot of count okay so we need to get the rates okay so in order for us to get the rates we need to
28:37
do something like something like this okay so this is actually the next step the next step which be processing the
28:43
date okay so so in order for us to get the uh to process the date and get all the information that we need we are
28:49
going to do something like this uh let's call it as USD rits okay
28:56
usd rates USD rates will be equal data
29:02
okay data JSON because this one is the one was the information okay and open
29:08
like this okay what we need we need only the rates okay because we need to get only this guy this rates perfect save
29:16
now if I print this guy okay come here save and let's run the guy boom if
29:23
we run ah he's always Good to clear it's always good to clean okay but it always run let's wait for result okay uh
29:30
because I already run i forgot to clear oh my internet just fail okay let's wait
29:35
my internet to come back ah come on how can you fail right now oh no my internet just failed okay
29:42
it went down uh is my data over or what let me check okay let me clear this one so that's why
29:50
guys please donate to the channel okay so that I can get internet and work on the things because I think my data just
29:55
finished but uh let's oh okay I think it just went down okay let me try again let
30:01
me run the thing okay run I lost connection yeah my internet
30:08
just failed but no problem let me see uh unfortunately my internet just uh I don't know it's fail I think it's
30:13
finished or something like that uh you guys really need to donate some stuff okay so that I can get internet let me
30:20
let me try in uh okay connected okay um if I run this let me see come on please
30:26
work internet come back okay I think it's still telling me that invalidation
30:34
ID internet problem okay uh my okay I just internet is working now okay as you
30:40
can see now it's giving this okay USD rates one something like this okay without that unnecessary information
30:46
that we don't need okay perfect so now the next step that we need to do is to
30:51
convert okay to do the rate conversion so this we just processed our data we just got the thing that we need so now
30:58
let's process okay in order for us to convert USD into euro let's do something
31:03
like this okay uh if we check here okay what is the exchange of eo what is the
31:10
abbreviation of a B C D E F G A B C D E
31:17
F E I found it e is this one okay so now let's convert USD into E okay so in
31:24
order for us to do that let's do something like this okay I'm going to comment it okay like u
31:32
convert USD to euro perfect this way okay so in
31:38
order to do that let's let's do some USD value okay USD value so uh to get the
31:45
USD value is very easy okay so what's the USD value that we want to convert so
31:50
we want to convert for them $50 okay into euros okay in order to do that let's create a new variable called euro
31:58
okay let's abrease just like this ear uh value okay
32:03
value will be equal USD this now is just basically USD value which is this guy
32:10
okay into uh USD okay into USD rates okay and USD
32:19
rates usd rate which is this information we need to get only the euro okay which this guy USD rates come here let's put
32:28
euro we need only euro perfect now if we print this
32:33
guy come here okay and if we print here come here stay here perto save and value
32:43
okay now if we run this let's run ah I forget to clean it again so as you guys
32:49
can see is giving us this value okay this 43.73 okay so now let's organize very
32:57
well this uh message okay so let's format it so for that let's do something like this print open and close print
33:04
what uh let's say something I'm going to use F string f string let's say something something like uh
33:12
USD USD value okay the value that we have and
33:17
then let's say USD will be equal open and close here let's give the euro e
33:26
value okay euro value i'm going to use fing to format okay let's format with
33:32
two house okay uh no dot to f this way
33:38
okay perfect save and come here and let's say euro perfect
33:45
save save okay let me clear this thing clear and if I run now let's wait for
33:51
the results okay it must give us Oh why I print this also okay but no problem
33:57
since we have this one it's okay i hope I don't lose connection again okay per what it gave it gave us F
34:06
string yeah it gave F string because I did a mistake okay this
34:12
guy uh this fs string yoke don't stay there okay and I need to come here and
34:20
close this something like this okay save that's why it gave us this something like that okay it's a mistake my mistake
34:26
save now let's uh run it again okay now I just correct okay this supposed to be out and this be like this okay perfect
34:33
now it give us okay 50 USD into arrow is equal
34:38
43.73 euro okay Perfect so guys we will stop here okay so this is how you can
34:44
work with JSON files in Python okay how you can convert how you can get from request and you can write delete a JSON
34:52
file in Python okay so my name is John if you like this uh tutorial this okay give a like and please also subscribe to
34:58
the channel give a like and donate if it's possible okay so that you guys can help me to improve more my stuff like
35:05
that as you can see while the tutorial going on I lost connection okay because my internet my data finished then I went
35:10
and asked to my neighbors just to share me a little okay of his internet okay so but thank you and see you in the next