Convert JSON to XML in Python

This post is lesson 47 of 54 in the subject Python Programming Language

XML and JSON are the two most commonly used data formats today. In Python, we can convert between these two data formats. This article will use the json and ElementTree modules to convert JSON to XML in Python.

Before reading this article, you should read the following articles about XML and JSON in Python:

1. Steps to convert JSON to XML in Python

To convert JSON data to XML data in Python, follow these steps:

Step 1. Import the json and ElementTree libraries.

import json
import xml.etree.ElementTree as ET
from xml.dom import minidom

We use the minidom module to create an XML string with tab indentation using the “\t” character to make the XML data easier to read.

Step 2. Read the data in the JSON file and convert it to a dictionary with the json.load() function.

Step 3. Use the ElementTree module to create XML tags. The attributes and text of the XML tags are accessed from the dictionary converted from the JSON data in step 2.

Step 4. Convert the XML data to a string and then write the XML string to a file.

2. Example of converting JSON to XML

Suppose, we have a file info.json with the following content.

{
    "domainname": "gochocit.com",
    "active": true,
    "numberposts": 360,
    "category": {
        "item": [
            {
                "@post": 50,
                "#text": "hardware"
            },
            {
                "@post": 150,
                "#text": "software"
            },
            {
                "@post": 17,
                "#text": "network"
            }
        ]
    },
    "facebookpage": "https://www.facebook.com/gochocit/",
    "build": {
        "language": "php",
        "cms": "wordpress",
        "database": "mysql"
    }
}

Below is the code that converts JSON data to XML data.

import json
import xml.etree.ElementTree as ET
from xml.dom import minidom

# read json file
with open("info.json") as json_file:
    data_json = json.load(json_file)

# create root tag
website = ET.Element('website')

# domainname tag
domainname = ET.SubElement(website, 'domainname')
domainname.text = data_json["domainname"]

# active tag
active = ET.SubElement(website, 'active')
active.text = str(data_json["active"])

# numberposts tag
numberposts = ET.SubElement(website, 'numberposts')
numberposts.text = str(data_json["numberposts"])

# category tag
category = ET.SubElement(website, 'category')
# item1 tag of category tag
item1 = ET.SubElement(category, 'item')
item1.set("post", str(data_json["category"]["item"][0]["@post"]))
item1.text = data_json["category"]["item"][0]["#text"]
# item2 tag of category tag
item2 = ET.SubElement(category, 'item')
item2.set("post", str(data_json["category"]["item"][1]["@post"]))
item2.text = data_json["category"]["item"][1]["#text"]
# item3 tag of category tag
item3 = ET.SubElement(category, 'item')
item3.set("post", str(data_json["category"]["item"][2]["@post"]))
item3.text = data_json["category"]["item"][2]["#text"]

# facebookpage tag
facebookpage = ET.SubElement(website, 'facebookpage')
facebookpage.text = data_json["facebookpage"]
# build tag
build = ET.SubElement(website, 'build')
# language tag of build tag
language = ET.SubElement(build, 'language')
language.text = data_json["build"]["language"]
# cms tag of build tag
cms = ET.SubElement(build, 'cms')
cms.text = data_json["build"]["cms"]
# database tag of build tag
database = ET.SubElement(build, 'database')
database.text = data_json["build"]["database"]

# create a pretty XML string
mydata = ET.tostring(website, encoding='unicode')
mydata_minidom = minidom.parseString(mydata)
pretty_mydata = mydata_minidom.toprettyxml(indent="\t")
# create a new XML file
myxmlfile = open("info.xml", "w")
myxmlfile.write(pretty_mydata)
myxmlfile.close()

Resulting info.xml file

<?xml version="1.0" ?>
<website>
	<domainname>gochocit.com</domainname>
	<active>True</active>
	<numberposts>360</numberposts>
	<category>
		<item post="50">hardware</item>
		<item post="150">software</item>
		<item post="17">network</item>
	</category>
	<facebookpage>
		https://www.facebook.com/gochocit/
	</facebookpage>
	<build>
		<language>php</language>
		<cms>wordpress</cms>
		<database>mysql</database>
	</build>
</website>

Note: Need to convert boolean and number data types in JSON to string using the str() function when converting JSON to XML.

Rate this post
Previous and next lesson in subject<< Convert XML to JSON in PythonHow to connect MySQL database in Python >>

Leave a Reply

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