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.


import_contacts

You may also like:

Related Posts

  • Python copy file to another directory
    17 January

    How to copy file to another directory in Python

    Table of ContentsWays to copy file to another directoy in PythonUsing file handlingUsing the shutil libraryUsing the pathlib libraryUsing the os moduleUsing the subprocess moduleConclusion In this article, we will see different ways to copy file to another directory in Python. We can read and write files in Python. We can also work with paths […]

  • Check if variable is String in Python
    13 January

    Check if variable is String in python

    Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many […]

  • Count the number of characters in a String in Python
    13 January

    Count number of characters in a string in python

    Table of ContentsWays to count the number of characters in a string in PythonUsing the len() functionUsing the for loopUsing the collections.Counter classConclusion In this post, we will see how to count number of characters in a String in Python. We can think of strings as a collection of characters, with every character at a […]

  • Pandas apply function to column
    12 January

    Pandas apply function to column

    Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() function We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over a specific column […]

  • Copy file Python
    12 January

    How to Copy File in Python?

    Table of ContentsCopy file using the shutil module in PythonCopy file using the copyfileobj() function in PythonCopy file using the copyfile() function in PythonCopy file using the copy() function in PythonCopy file using the copy2() function in PythonCopy file using the operating system command in PythonCopy file using the os module in PythonCopy file using […]

  • What is percent in python
    11 January

    What is % in Python?

    Table of ContentsWhat is % Operator in Python?The % Operator with strings in pythonThe % Operator as a placeholder for variables  in pythonThe % Operator as format specifiers in pythonConclusion In this article, we will cover what is % in Python and what are different ways to use % in Python. In python, there are […]

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.