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
before that, okay, let us define what is JSON. So JSON stay for JavaScript object notation. Okay, we wrote this way. Okay,
JSON. So this GS here, the first GS, okay, it stay for JavaScript and O stay
for object and N stay for notation. Okay, so it is a lightweight and widely used data format for storing data and
exchange information. So despite his name being like JSON okay and the JavaScript being here okay in the first
JSON it is independent and supported by many programming language okay including Python also. So in this video we will
try to know better. We will explore how we can work with JSON data in Python from the basics itself. Okay. And we
also going to have some kind of a practical experience by accessing data from any API. Okay. Any free API. So but
as I said okay we will learn how we can work with JSON data like writing the JSON data we can load JSON data we can
convert the JSON data into Python object. So and many things on okay. So bad. But before 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 key value pairs
and order lists. It is similar to the Python dictionary, okay, and list also. So here is one example of JSON data. So
this is a simple structure of a JSON data. Okay, as I said, okay, it looks like a donar and list in Python. Okay,
so we have this pair of open brackets and here inside we have some kind of list. Okay, so this is the object that
we will work on. Okay, for example here people represent object. So this object people it has a lot of datas inside okay
or a lot of uh lists of dictionary. Okay, for we have the first information
the first data which is this one and it is separ separated by comma which is this one. Okay, and the second data is
this one. Okay, which is also separate. Okay, the this comma separates the both
of the data. Okay, so we will learn how we can work with this thing. Okay, with this type of extractors data and the
first operation that we are going to perform here will be converting okay between JSON and Python object. So in
order to do that okay we need to use JSON for that. Okay. So how we are going
to do this? 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 the built-in model which is JSON by from Python. Okay. It allows easy conversion between JSON strings and
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 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 to import JSON inside of our script. Okay, I'm going to do something like this. Okay, import JSON. Import
JSON. So this way we import JSON into our script. So to in order to convert
this thing, okay, first we will do something like this. I'm going to create any variable. I'll
call it as a JSON. JSON data. JSON data will be equal. Okay. So this
JSON data will be inside of uh uh quotes which will be okay. 1 2 3 this
way. Okay. I'm going to open here and I'm going to copy all of this thing inside of that quartz. Okay. I'm coming
here. Let me give a little space and boom. I will do something like this. So
this way I just put the all this information. Okay, inside of this variable also like this. If I going if I
print 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 which is this one. Save. And if I run this script, let me show
you. As you can see, it just print all this extract. Okay, so this is not yet
converted into Python object. Okay. So for us to convert this structure, okay, into Python object, we need to do
something like this. Okay. Come 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. 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 way here. Dot loads uh open
and close. What I'm going to load? I'm going to load this information that I have here. Okay. which is JSON data. So
I'm going to come here JSON data. Okay, JSON data. Cool. So now this way
if I print this Python data. Okay, come here. Let me print it. Put here. If I
save and if I print it, let me push this up.
Now if I print this thing, okay, let's print. Now first let me clear here so that we can see the things properly.
Let me clear this clear. Now if I print this thing, okay, let 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 people as a key itself. Okay.
Then we have a list of dictionaries. Okay. So this is the first dictionary which start from here and stops here.
Then we have the comma and this is the second dictionary. So this is the way that you can convert any structured data
JSON data okay into Python object. So now the next operation that we are going to perform will be create read and
modify a JSON file. Okay. So now we are going to work with JSON file. So for this okay we are going to need something
like this. Okay initial. 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 you can make use of it. Okay. So this is for me to don't type. Okay. because if I
type this might take long time so 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 first operation that we are going to do
is to create a JSON file by using Python 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 JSON this way as always okay save so
after importing JSON okay we have this information here Okay, in order for us to create any JSON file by uh using
Python, it will be something like this. See,
first I'm going to open this. Okay, I'm going to open any file. Okay, I'm going
to open the file with Python. Okay, so it's like that. If the file that I'm going to open exist, it will open. Okay,
if it doesn't exist, it will create that file, then it will open it. So how I'm going to do this is it it will be
something like this. See with okay with open with open what? So I'm going to
give the name. Okay. So this is okay. This information is about the states. Okay. The states of Brazil. Okay. So for
them S. Paulo is the one state of Brazil. Rio de Janeiro is one state of Brazil. Mina Bay and Parano. Okay. Those
are the information of states of Brazil. Okay. So this is the abbreviation of the state of sample which which is SP and
this is the area code which is 11. Okay. So and the same goes with Rio Janeiro Min and 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 do JSON this way. Okay.
States.json. So since I'm opening this okay since I
want to create a new uh JSON file. Okay. I'm going to open this in writing mode.
Okay. So for writing mode I need to give w which is for writing mode. 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 important for you to give encoding. Okay. Because if
you don't give encode it might get error while open the file. So in order for the things to open properly is very useful
is very necessary for you to use encoding. So that's why we are going to use encoding. UT uh UT what UTF-
and 8 then we are going to consider all this thing as F okay then the dot I'm
going to come the next line so after this okay I'm going to use JSON
dump okay so JSON dump is the one that help us to open a JSON JSON file okay in
Python so in order for me to do that I'm going to do this okay JSON okay JSON dot
dump this way. Okay, JSON.dump. I'm going to open. So the first thing that I'm going to give is the information
says which is the data. Okay, so the data is this one. Okay, the data initial. So I'm going to come here down.
Okay, I'm going to paste data initial which is this guy. Okay, 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 this F fa.
And then I'm going to use something which is really very important. Okay, that help us to keep the JSON file well
formatted. Okay, very indented. So which is this one I indent. Okay, which is this indent. I'm going to give two.
Okay, so that it will format the file very well. Okay, like giving two space. So after that, okay, there is another
par that we are very important for us to give which is this one. Okay, insure ask. So in should ask is for okay for
them there are some words okay which has some kind of special character something like this for them if you check is s
Paulo okay s it does something here in the top of a that in Portuguese we call it as a title okay so this is kind of a
special characters character so in order for us to open or to create or to save this information properly okay without
any 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
will open the things or it will save the things properly without any damage of the information. Okay. So this also goes
for special character like for if you have like a robot if you have some any kind of special characters okay in
alphabet is very important for us to give this insure ask then we just need to give like fails something like this
okay fails so after that okay this way I'm just I'm creating the I'm writing
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
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 I'm going to open? Okay. Let me do this. Boom. H I'm going to up. Okay. File. File. Okay. File. Okay. This this
is just a name that I'm just going I'm giving. Okay. So that it gain the f that
we can see the okay. File states state status dot JSON states.json. Okay. Oh,
sorry. State.json created. Okay. Let's say created create
created. Okay. So this is just for us just so that we can get any information that okay the file was created. Okay. So
after that let me give one kind of slash N so that can format well my input.
Okay. So my output better. Okay. Let me clear this. Clear. Clean. Clear. Clean.
Uh oh. It's clear not clean. I need Okay. Now I have to waited for process. Okay. Exactly. Is clear not
clean. Clear. Okay. Let me clear this. Now let me run this uh piece of code so that we can see what if I run this as
you can see it print me file state.json JSON created as we can see now we have here one file called states dojson okay
if I'm coming if I click there and as you can see it just created this file okay as you can see states okay then we
have name s Paulo abbreviation of s area code 11 name and so on so so on so on
okay so it just create our JSON file so it works like that okay if uh the file
exist it will like write inside of that file okay if it doesn't exist it will
create the file file then it will post it will save the information there. So those information we just save as a JSON
file as you can see even the icon okay is kind completely different okay it's kind of like open bracket brackets open
bracket and close bracket that is how JSON file are identified so the next operation that we are going to perform
will be how we can read and assess data okay JSON data in Python so 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 I'm just going to
comment this uh line of code. Okay, I'm going to comment this. Okay, even this
one. Okay, let me comment them. Yeah, they stay together. Boom. So, okay, in
order for us to open to read and assess the data, we need to do something 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
assess? We need to assess this this uh data so that we can do some manipulation. Okay, let me assess
states. Okay, is this states states do JSON state JSON and since I'm
open it for reading okay so it means I'm going to give the read parameter okay
which is R this way okay R allows us to read the information so after that okay
let me use also encoding I will never forget encoding okay it's very really really important to use encoding
encoding what I'm going to give as always okay UTF F plus hyphen and 8. So
if this thing let's consider it as f okay dot so we are reading the
information okay so for read information I'm going to create a variable called data okay so this data will be the the
information that I'll be manipulating so data so for let's use now JSON dot okay
to open we need to just use the load function okay which says like this save
I did a mistake here okay is JSON JSON
JSON.load and I forgot to give the F. Okay, which is the file JSON.load F
save. Perfect. So now we have the data here. Okay, so we have this data uh which is
which contains all this information here. Now let me close this. Okay, close that one. So come here. Perfect. So now
uh the 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 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's see states okay status and
abbreviation abbreviations abbreviation one B is missing the abbrevi abbreviations state and
abbreviations okay dots oh sorry dots and save so
I'm going to some kind of final meaning blah blah that's not important Okay, states and abregation. Okay, come here.
So after that, okay, I'm going to do something. Okay, for us to assess the
the 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 state only for state in in what in data which is this information.
Okay, for states in data and let's use this. Okay, so we just need to assess the states and the abbreviation. Okay.
So if I come in into this state uh this JSON information, okay, I'm going to assess this stage which is the object.
Okay. Because this object state has all this information. Okay, let's let me assess status. Come here. Okay, I'm
going to give here like this. Okay, status. Okay, for state in status I'm
going to do this dot. What I need? I need to print print open. Okay, I need
to print I'm going to use f string. Okay, f let me use f string then I'm going to print. Okay, let me do this.
Okay, state I'm going I'm using this state. Okay, state h what I need to assess I just need to
assess. Okay, since we are here when I give data states okay which means I I
already got all this information. Okay, so inside of this information now I need only the name the name of the state.
Okay, I'm going to use the name state name. Okay.
State name and the abbreviation also. Okay. Camil give
I'm going to open bracket parent this way. Okay. Then let me get the state.
Okay. State. I need the abbreviations also. Okay. What is the abbreviation? Abbreviations.
So this is how you can assess or you can manipulate the JSON data in Python. Okay. I'm going to use the abbreviation
save. Now if I run this link okay let's run this
down if I run this guy let's run this save
and run boom as you can see we got the states s Paulo what's the abbreviation
of s Paulo SP if we come here and check as you can see uh s Paulo what's the abbreviation of s paulo sp okay okay and
we got for all the states okay s paulo janeiro miner bay and parana and so on
so this is how we can read and access the data in JSON file list by using
Python. So the next operation which is also very really really good important uh information operation is removing
area. Okay, removing data inside of a JSON information uh data. So in 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 for state in in what in data
uh data what let's assess only states okay states let's do and dot okay like
this boom perfect so and we want to remove the
the operation that we're going to use here is to remove the area code okay we want to remove this this one okay this
area code in this information Okay. So, as we know, okay, is always good when
you are manipulating data, okay, never mess up with the original data, okay, because you might need this. So, when
you are doing manipulation of data, okay, always create a copy, create a new data that will put this uh that will
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 check. Okay, let's check if area code exist. Okay, if area okay if area
code come here okay in state if area code in
state okay let's do this dot and come down so what we going to do is okay we are going to delete delete what delete
state okay state area code delete state area code perfect save
okay so like that if I run this okay let me run boom uh it just it's 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 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 like this see and
let's reuse this guy okay this code that we have here okay let's reuse them I'm
just going to copy come here okay so after this operation which is removing operation Okay. Uh I'm
going to paste these guys here. Come here. Okay. Delete this and 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 date. Okay.
Let's call it like a new states, okay? JSON. Let's open the same
way. Let's use encoding as always. And here also let's keep like this. Okay. So what we are going to
to give it, okay, so we are not going to do with data initial. Okay. which data 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 remove this. Let's give this one
data and here let's keep the way that it is. Okay. So now let's print the information and let's say okay file
states okay file uh new states file new
new states. Save it. Save it. Save it without. Let's use without.
Okay. Save it without. With without Save it without area
without area code. Save.
So for that I need to print it. Okay. So for me to print it uh it's very easy. I just I can just reuse this one also.
Okay. And I can just reuse this one. Okay. I'm going to copy this information
that piece of code coming down and paste it. Okay. So, what I'm going to do for
data in uh no no I need to assess this one.
Okay. So, for that I need to grab all this too. I need to get this full
come here. Okay. And okay. So, because I need to open the new information. Okay.
The new information will be this one new states JSON come here okay instead of this let me give okay new state JSON 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 this
okay so this is okay now let's come here okay
and into this one okay let's this one let's print with area code okay let me
add also one more thing and just let me come here I'm going to copy this one
give a little space I'm going to give like right like this paste and now I'm going just to give that area code okay
so this one it will print with area code and another one will print without area code okay save okay perfect now this one
will print without area code okay save and if I run this let me come it up if I
run the code let me see boom as you can see everything work okay so this is the first ones okay with area code say Paulo
SP abbreviation 11 the area code. Then we have okay file a new state JSON ser
without area code as you can see states and abbreviation S Paulo SP something like that okay and also
it created a new file as you can see new states okay if I come in this new states and if we check that new states so let
me come this guy down if you say new state name s Paulo abbreviation SP there is no area code okay but this old one
has area code okay so guys this is how you can manipulate JSON data by using
Python. Okay, so and the name operation that we are going to perform it's not
operation. Okay, so this is something we which is very good that we are going to do So we are going to try to access any
information from any API which exchange rates API and let's work with that thing
and let's see because that is a very really really good example okay it's a practical example okay with information
from card exchange API integration okay so let's do this thing and then we can
close the our projects okay so in order to do that let's do something with this okay I'm going to use this one let me
clear this so and Let's uh consume a data from the
exchange rate API. Okay. So in order for us to do that, okay, let's do something
like this. Uh I'm going to import JSON itself first as always. Okay. Import
JSON. Import JSON. Oh, JSON. Import JSON. Oh, let me give a
little Okay, this way is good. You guys can see properly. Import JSON. So after import JSON, uh
what I'm going to do? So the next operation the next thing to do will be uh control B let me I think to be like
this will be better import JS okay the next operation that we need to do I'm going to use from ur
okay from url lib from url lib import what I'm going
to import uh url lib request dot request
okay request import I'm going to import URL open your RL open. Perfect. So
the first thing that we need to do is oh sorry the first thing that we need to do
the first thing that we need to do is to fetch data from 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 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 use this one. Okay. http http s dot dot what and slash
something like this then say open eer okay api
okay.com okay okay underscore like this v6
okay this is one end point that we can get the latest information okay latest
and underscore again USD. Okay, we can get the latest uh information of USD
from this URL. Perfect. So, save. Now, if I print this guy, okay, print. Let's
print what? URL. Perfect. Save. And let me clear it. Clear.
And if this guy come up, okay, save. Now, let me run. As you can see, it's
just printing the URL itself. Okay. So, after that, the next thing that we need to do is let's get the response. Okay.
response 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 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 me copy and paste here boom save and if we run this guy let's see what's
going to give us boom as you can see it gave us httpclient http response object something like this okay perfect so this
UR URL okay it give us also the information as a JSON file okay so in
order for us to open this information properly okay let's use the JSON uh JSON
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
will be equal JSON dot loads
open open what let's open the response that we got it okay response
dot dot read this way. Okay, open and close. Then dot decode. Okay, because we
need to decode this information so that we can see it properly. Okay, decode open and close and do this UTF. Okay,
underscore uh not that's not underscore. Okay, hyphen 8. Perfect. Okay, save. So
now if we print this data JSON, copy come here. Okay, and boom. Save. Now
let's clear this so that we can have more space. Let's clear if we do this save and if we run let's wait since
because it's going to assess the URL and it might take a little time because my internet is very slow. Okay. And if you
guys want to help the channel please you can donate to help me so that I can improve some stuffs here. As you can see
it's give us this information okay as results result success provider this one
documentation. So this is a JSON uh this is a Python object okay that it 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 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 for uh
Arabian USD for Afghanistan for all for AMD for NJ is my NJ I don't know which
country is this but this A O A A is my count which is Angola and BZ 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 do
something like something like this. Okay. So this is actually the next step. The next step which will be processing
the data. 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
going to do something like this. Uh let's call it as USD reads. Okay. USD
rates USD rates will be equal data okay data JSON because this one is the one
was the information okay and open like this okay what we need we need only the
rates okay because we need to get only this guy this rates perfect save now if
I print this guy okay come here save and let's run the guy boom if we
run ah it's always to clear. It's always good to clean. Okay, but it always run. Let's wait for result. Okay, because I
already run. I forgot to clear. Oh, my internet just fail. Okay, let's wait my
internet to come back. Ah, come on. How can you fail right now? Oh no, my internet just failed. Okay, it
went down. Uh, is my data over or what? Let me check. Okay, let me cl this one. So that's why
guys, please donate to the channel, okay? so that I can get internet and work on the things because I think my data just finished but uh let's oh okay
I think it just went down okay let me try again let me run the thing okay run
I lost connection yeah my internet 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 finished or something like that uh you guys will need to donate some stuff okay
so that I can get internet let me let me try again. Okay, connected. Okay. Um, if
I run this, let me see. Come on, please work. Internet, come back. Okay, I think it's still telling me that invalidation
ID internet problem. Okay. Uh, my Okay, I
just internet is working now. Okay. As you can see now, it's giving this. Okay. USD rates one something like this. Okay.
Without that necessary information that we don't need. Okay. Perfect. So now the
next step that we need to do is to convert okay to do the rate conversion. So this we just processed our data we
just got the thing that we need. So now let's process okay in order for us to convert USD into euro. Let's do
something like this. Okay. Uh if we check here okay e what is the exchange
of eo e. What is the abbreviation of e? A B C D E F G A B C D E F L E I found it
here. Here is this one. Okay. So now let's convert USD into E. Okay. So in order for us to do that, let's do
something something like this. Okay. I'm going to comment here. Okay. Like convert
USD to Euro. Perfect. This way. Okay. So in
order to do that let's let's first of all USD value okay USD value so uh to
get the USD value is very easy okay so what's the USD value that we want to
convert so we want to convert for them $50 okay into euros okay in order to do
that let's create a new variable called euro okay let's just like this year uh
value okay value will be equal USD this Now it's just
based mathematically USD value which is this guy okay into
uh USD okay into USD rates okay uh USD 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
euro we need only euro perfect now if we print this guy
come here okay and if we paint here. Come here. Stay here. Perfe safe. Uh
value value. Okay. Now, if we run this, let's run. Ah, I forget to clean it
again. So, as you guys can see, it's giving us this value. Okay. This 43.73.
Okay. So, now let's organize very well this uh message. Okay. So, let's format it. So, for that, let's do something
like this. Print open and close. Print what? H let's say something I'm going to
use F string. F string let's say something something like USD
USD value okay the value that we have and then
let's say USD will be equal open and close let's give the e
value okay value and I'm going to use fing to format okay let's format with
two house okay uh no dots two F this way. Okay, perfect. Save. And come here.
And let's say Europe. Perfect. Save.
Save. Okay. Let me finish this thing. This. And if I run now, let's wait for the results. Okay. It must give us Oh,
why? I print this also. Okay. But no problem since we have this one. It's okay.
I hope I don't lose connection again. Okay. Perfect. Oh, what? He gave it gave us F string. Oh yeah, it gave F string
because I did a mistake. Okay, this guy uh this F string yoke don't stay there.
Okay, and I need to come here and close this something like this. Okay, save.
That's why it gave us this something like that. Okay, it's mistake. My mistake. 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. know it give us okay 50 USD into arrow is equal 43.73
e okay perfect so guys we will stop here okay so this is how you can work with
JSON files in Python okay how you can convert how you can get from request and
you can write delete a JSON file in Python okay so my name is if you like this tutorial this okay give a like and
please also subscribe to the channel give a like and donate if it's possible okay so that you guys can help me to
improve more my stuff something like that. As you can see while the tutorial going on I lost connection okay because
my internet my data finished. Then I went 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 video. Bye.