Sai A Sai A
Updated date Apr 03, 2024
In this blog, we will discuss multiple methods for converting XML to JSON in Python, including the built-in xmltodict library, ElementTree, lxml, and dicttoxml.
  • 1.6k
  • 0
  • 0

Convert XML to JSON in python:

In this blog, we will discuss how to convert XML to JSON in Python. We will cover multiple methods, including the built-in xmltodict library, ElementTree, and lxml

Method 1: Using the xmltodict library

The xmltodict library is a Python module that allows you to parse XML data and convert it to a dictionary-like object. The library is easy to use and supports both Python 2 and 3.

To use xmltodict, you first need to install it using pip. 

pip install xmltodict

Once you have installed xmltodict, you can use it to convert XML to JSON as follows:

import xmltodict
import json

xml_data = """
<book>
  <title>Python for Data Science</title>
  <author>John Smith</author>
  <price>29.99</price>
</book>
"""

json_data = json.dumps(xmltodict.parse(xml_data))
print(json_data)

In the code above, we first import the xmltodict and json modules. We then define an XML string and use the parse method of xmltodict to convert the XML string to a dictionary-like object. We then use the dumps method of the json module to convert the dictionary to a JSON string. Finally, we print the JSON string.

Output:

{"book": {"title": "Python for Data Science", "author": "John Smith", "price": "29.99"}}

Method 2: Using ElementTree

ElementTree is a Python module that provides a simple way to parse and manipulate XML data. It is included in the standard library, so you don't need to install any additional libraries.

import xml.etree.ElementTree as ET
import json

xml_data = """
<book>
  <title>Python for Data Science</title>
  <author>John Smith</author>
  <price>29.99</price>
</book>
"""

root = ET.fromstring(xml_data)
json_data = json.dumps({root.tag: {child.tag: child.text for child in root}})
print(json_data)

In the code above, we first import the ElementTree and json modules. We then define an XML string and use the fromstring method of ElementTree to parse the XML data and create an Element object. We then create a dictionary using a dictionary comprehension that iterates over the children of the root Element object. We then use the dumps method of the json module to convert the dictionary to a JSON string. Finally, we print the JSON string.

Output:

{"book": {"title": "Python for Data Science", "author": "John Smith", "price": "29.99"}}

Method 3: Using lxml

lxml is a high-performance XML and HTML processing library that provides a Pythonic API for working with XML data. It is built on top of the libxml2 and libxslt libraries, which are written in C.

import lxml.etree as ET
import json

xml_data = """
<book>

  <title>Python for Data Science</title>
  <author>John Smith</author>
  <price>29.99</price>
</book>
"""
root = ET.fromstring(xml_data)
json_data = json.dumps({root.tag: {child.tag: child.text for child in root}})
print(json_data)

In the code, we first import the lxml.etree and json modules. We then define an XML string and use the `fromstring` method of lxml.etree to parse the XML data and create an Element object. We then create a dictionary using a dictionary comprehension that iterates over the children of the root Element object. We then use the `dumps` method of the json module to convert the dictionary to a JSON string. Finally, we print the JSON string.

Output:

{"book": {"title": "Python for Data Science", "author": "John Smith", "price": "29.99"}}

Method 4: Using dicttoxml

dicttoxml is a Python module that allows you to convert a Python dictionary to an XML string. You can then use one of the methods discussed above to convert the XML string to JSON. Here's how you can use dicttoxml to convert a dictionary to XML and then convert the XML to JSON:

import dicttoxml
import xmltodict
import json

data = {
    "book": {
        "title": "Python for Data Science",
        "author": "John Smith",
        "price": 29.99
    }
}

xml_data = dicttoxml.dicttoxml(data)
json_data = json.dumps(xmltodict.parse(xml_data))
print(json_data)

In the code, we first import the dicttoxml, xmltodict, and json modules. We then define a Python dictionary and use the dicttoxml method of dicttoxml to convert the dictionary to an XML string. We then use the parse method of xmltodict to convert the XML string to a dictionary-like object. We then use the dumps method of the json module to convert the dictionary to a JSON string. Finally, we print the JSON string.

Output:

{"book": {"title": "Python for Data Science", "author": "John Smith", "price": "29.99"}}

Comments (0)

There are no comments. Be the first to comment!!!