Convert XML to JSON in python

Python XML to JSON

XML (Extensible Markup Language) can create elements to store data. It is used for exchanging structured data over APIs and applications. JSON (JavaScript Object Notation) is also a very commonly used data structure notation to transfer the data between APIs and web applications. It is based on arrays and dictionaries. It was built as an improvement over XML.

The Python programming language has modules available to parse both JSON and XML data. There is no direct method to convert XML to JSON so we will use a dictionary as an intermediary. We will learn how to perform this conversion in this article.

Using the xmltodict and json module to convert XML data to JSON

The xmltodict is a module used to read and parse XML data to dictionary and list type structures. We can use the xmltodict.parse() function from this module to achieve this.

We can then use the json module to write this data to JSON type. The json.dump() function can write the given object to a JSON string.

See the following code.

Output:

{“emp”: {“id”: “102”, “name”: “Mark”, “dept”: “Accounts”, “sal”: “75000”}}

In the above example,

  • We created an object which stores the XML string.
  • The xmltodict.parse() function parses this to a dictionary type object.
  • We pass this object to the json.dump() function which returns a JSON formatted string.

Using the xml.etree.ElementTree and json module to convert XML data to JSON

The xml.etree module is an efficient method to parse the XML data as a tree. We can use this module to create a user-defined function that will parse our XML string to a dictionary, which we can write as a JSON file using the json module.

See the code below.

Output:

{“emp”: {“id”: “102”, “name”: “Mark”, “dept”: “Accounts”, “sal”: “75000”}}

Now, let us understand what we implemented in the above code.

  • We pass the XML string using the ET.XML() function.
  • We pass this data to a function called xmldict().
  • We process this data to return a dictionary.
  • This dictionary is then passed to the json.dump() function, which returns the final JSON data.

Using the xmljson and json module to convert XML data to JSON

The xmljson is a new and simple library available to process XML data in Python. It provides different objects to parse the data differently.

We can use the xmljson.Yahoo() constructor to initialize an object that can be used to work with XML string. The xtree.ElementTree.fromstring() function returns an XML string, and we use the data() function with this object to parse this string. This will return a dictionary with the required data.

Then as we did earlier, we convert this dictionary to JSON using the json.dump() function.

For example,

Output:

{“emp”: {“id”: “102”, “name”: “Mark”, “dept”: “Accounts”, “sal”: “75000”}}

That’s all about how to convert XML to JSON in Python.

Related Posts

  • Python add commas to number
    11 September

    Python add commas to number

    Table of ContentsUsing the format() function to add commas to numbers in PythonUsing the fstrings to add commas to numbers in PythonUsing the regular expressions to add commas to numbers in PythonUsing the locale module to add commas to numbers in Python Python allows us to format values to get the final result in our […]

  • Write array to CSV in Python
    30 August

    Python write array to CSV

    Table of ContentsUsing the pandas.to_csv() function to write array to CSV fileUsing the numpy.tofile() function to write array to CSV fileUsing the numpy.savetxt() function to write array to CSV fileUsing the file-handling methods to write array to CSV fileUsing the writerows() function to write array to CSV file A CSV file is a simple text […]

  • Multiply in Python
    30 August

    Multiply in Python

    Table of ContentsMultiply operator in PythonMultiplying a string with an integerMultiplying all the elements of a listUse the for loop to multiply all the elements of a listUse the functools.reduce() function to multiply all the elements of a listUse the numpy.prod() function to multiply all the elements of a listUse the math.prod() function to multiply […]

  • Auto clicker in Python
    30 August

    How to make auto clicker in Python

    Table of ContentsUsing the pynput module to create an auto clickerUsing the pyautogui module to create an auto clickerUsing the pywin32 (winauto32api) module to create an auto clicker Python has inbuilt modules and methods which allow it to detect and perform keyboard and mouse inputs. We can create an auto-mouse clicker in Python. An auto-clicker […]

  • Iterate through files in directory in Python
    30 August

    Iterate through files in directory in python

    Table of ContentsUsing the os.scandir() function to iterate through files in a given directory in Python.Using the os.listdir() function to iterate through files in a given directory in Python.Using the os.walk() function to iterate through files in a given directory in Python.Using the pathlib module to iterate through files in a given directory in Python.Using […]

  • Write variable to file in Python
    30 August

    Write variable to file in python

    Table of ContentsUsing the str() functionUsing the repr() functionUsing the string formattingUsing the pickle.dump() functionUsing the numpy.savetxt function File handling is an important concept of programming. Python allows us to read and write content to an external file. In this article, we will discuss several ways to write variable to file in Python. First, let […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.