How to Convert XML to JSON using Python!

LeMaster Tech
183 followers
10 Video Views·Apr 20, 2025
Data can come in a variety of formats in the programming world, and knowing how to convert between them is extremely useful! In this video we show how to convert from XML data to JSON format using a simple Python script and we do it two different ways using the XMLtoDict Library and also the XML.ElementTree! Either way works great and should get you the output you need!
code available here: https://github.com/plemaster01/xmltojson
Thanks for watching, don't forget to leave a like on the video and subscribe to the channel - and drop a comment letting me know what you'd like to see next on the channel!
Massive thanks as always to Patreon Supporters Dale Topley and Philip Nunez!
https://www.patreon.com/LeMasterTech
welcome back to L MasterTech in today's
video I'm going to show you how to take
the data from an XML file and convert it
into a Json format using a python
[Music]
script okay this is a really simple
concept um but it is uh something really
useful that comes up fairly often um
converting from one message or code
format type into a more usable or more
easily parseable format um so I'm going
to start by just importing pretty print
because when I uh when I do any work
with Json I think that's useful to have
it just automatically formats it for us
and now I'm going to import two modules
the XML to dict dict module and the uh
Json module and if you don't have XML to
dict you might have to do pip install
XML to dict but basically uh it does
exactly what it sounds it takes XML data
and it converts it into a dictionary and
then the Json module is just going to
turn that dictionary into a Json for us
um so what we're going to do and my file
is just called file. XML and basically
it's got a note uh to Pete from Jack
reminder don't forget to like And
subscribe so two from heading body note
that's the substance of my XML file
That's How we'll know if it's working um
so what we do is we say with open and
then the name of our file and if you
don't have it in the same folder as
yourp file just make sure to um like
steer it to the correct folder and
directory and then we'll say uh with
opening that as and we'll call it our
XML file the data comes out as a
dictionary using XML to dict do parse
and it has this super easy uh built-in
parsing and all we have to do is say XML
file . read like that and uh I'm serious
that's like the whole thing here so what
we can do then is we can say the data in
Json format is using that Json module
and the command is dumps uh and then we
just give it that dictionary data and
that's um going to give us the format
into uh Json so then what we can do is
we can say with open and the reason with
open is awesome is if the file doesn't
exist yet whoa if the file doesn't exist
yet goes ahead and it makes it so what
we can say is with data. Json so that
file in a Json format um using the W so
the right command as our Json file then
we'll say Json file. write all of our
Json data okay so what this is doing is
it's saying open data. Json with the
intention to write and it's really nice
that file doesn't exist yet in our file
structure structure and so I'm going to
write all of this data to a file called
data. Json and create it and store it if
it doesn't exist yet and if it does
exist um then I'm going to overwrite it
with the new data and then what we'll do
just to make sure that it's working um
is we will just prettyprint that Json
data okay and this little 14 uh lines
here should do everything we need it to
do it should take that XML file and turn
it into a Json and what you can see down
in the the Bottom now is this really
nice note to Pete from Jack heading
reminder uh body don't forget to like
And subscribe and it's in this
dictionary uh format but then we also
over here on the side now we have data.
Json and that one didn't uh you know
Auto pretty print for us but that's fine
no to Pete from Jack heading reminder
body don't forget to like And subscribe
okay as a from a file. XML into a data.
Json so that's really great um that's
cool that's how easy it is and if that's
all you needed out of this video you can
stop right now uh if you are the Curious
type let's go ahead and do it one uh
extra way whoopsy daisies um this is
possibly even easier um but I saved it
for a second cuz I kind of like the
first one more what we're going to do
for this one I've just went ahead and
basically commented everything except
pretty print out um what we're going to
do for this one is we're going to use
the buil-in xml. tree basically so um
element tree and I'm going to import
that whole thing is e uh and then I'm
just going to define the tree the root
and an empty dictionary okay so what I'm
doing here is I'm using the xml. tree.
element tree um and I'll just refer to
it as e in the rest of this code and
tree equals E.P parse and then file. XML
okay so the tree that I want to use
element tree with um is a parsing of the
XML file and then the root is going to
be tree do get root okay so it's got a
built-in function to kind of sniff out
the root um which in this file is note
okay and then dick is empty dick I'm
saying DIC uh instead of di because di
is a python like built-in and you
shouldn't use that okay now what we have
to do is we have to go through all the
Childs in the root by saying for child
in root and we say if child. tag so the
tag uh you know two from uh header
anything like that and if that's not in
my dictionary already in my dick uh then
I'm going to put the child tag in the
dictionary and I'm going to set it equal
to the child text okay so basically the
two things that every tag or uh the two
things that every has there are the Tag
in the text and so I'm going to put
those in my dictionary and then what
we'll do is we'll prettyprint that
dictionary and now we should be able to
see it work if we run this again okay
and look at what we got down here body
don't forget to like And subscribe from
Jack uh heading reminder to Pete so the
uh order gets changed in this format and
I think maybe that's another case for
doing it in option one but I wanted to
show you here is this is not a
particularly hard concept um and just
here within basically 5 minutes we've
found two different ways of doing it but
it's super
useful okay again like parsing data from
one format to another comes up all the
time in industry and in software
programming so hopefully you found this
useful keep it short and sweet uh always
nice to knock one of these out consider
leaving a like on the video subscribing
to the channel let me know in the
comments below what you'd like to see
next or if you have any questions about
what you saw here today thanks so much
for watching as always good luck with
your projects till next time
bye
