Furthermore, XML is an HTML type markup language that is also used to store the data. But, the XML does not provide any kind of predefined tags. We can create our own tags and store the data. As we discussed, both JSON and XML are used to exchange data between the servers and software applications. However, the two data formats differ somewhat. The JSON is an object type data storage format, whereas XML has no type. The XML files only store the data in string format and heavier than the JSON file. Whereas the JSON files can store the string, arrays, numbers floating-point numbers, and Boolean.
This article explains XML to JSON conversion using Python. Python xmltodict module is used to convert the XML to JSON format.
The xmltodict module installation
Before start converting XML to JSON, we need to install the xmltodict module. The xmltodict module can be installed using the python index package (pip), and it can be installed in Python 2 and 3. In the case of pip2, execute the following command to install the xmltodict module:
If you are using pip3, then run the following command to install the xmltodict module:
In the case of Debian based system, then run the following command to install the xmltodict module:
The above-given command is suitable for Python2. In the case of the Python3 version, run the following command:
XML to JSON conversion
Now let’s convert the XML data to JSON format. We will use the xmltodict and JSON module for this conversion. The json is a built-in Python module. Therefore, the need to install them is eliminated. The xmltodict.parse() function converts the XML data into Python dictionary. Then, the json.dumps() function takes the converted dictionary object as an argument and further convert it into the JSON format. So this is a two steps process:
First we have to convert the XML into Python dictionary object using xmltodict.parse() function.
Secondly, we convert Python dictionary object to JSON format using json.dumps() function. In json.dumps() function, the indent property is used to add spaces between the data.
#declaring the xml
my_xml = """
<Name>Les Jardins du Marais</Name>
<Name>Golden Tulip Little Palace</Name>
#coverting xml to Python dictionary
dict_data = xmltodict.parse(my_xml)
#coverting to json
json_data = json.dumps(dict_data, indent=2)
The output shows that the XML is successfully converted to JSON format.
XML file to JSON file conversion
The XML files data can be converted and saved into the JSON file. Let’s open the XML file, convert the XML data to JSON and store it in a JSON file.
The following is the XML file.
# opening the xml file
with open("hotels.xml","r") as xmlfileObj:
#converting xml data to dictionary
data_dict = xmltodict.parse(xmlfileObj.read())
#creating JSON object using dictionary object
#storing json data to json file
with open("hotels.json", "w") as jsonfileObj:
The Python interpreter does not show any error; it means that the JSON data is successfully saved in a .json file.
XML and JSON are two popular data formats to store data. The XML data can be converted into JSON format using the xmltodict and JSON module. This article explains the XML to JSON data conversion with examples.