Extensible Markup Language (XML) and JavaScript Object Notation (JSON) are two famous data formats for the storage of data. Both JSON and XML allow us to store the data in such a way that is readable by human as well as machines. To begin with, JSON is a type of data structure that is mainly used to exchange data between servers and software applications. It stores the data as key-value pairs. JSON makes an object of the data where key-value pairs are separated on the basis of the colon (:), and one complete key-value pair separated by others based on the comma.
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.
import xmltodict
import json
#declaring the xml
my_xml = """
<Paris>
<Hotel>
<ID>1</ID>
<Name>Les Jardins du Marais</Name>
<Star>3</Star>
<Facilities>Internet</Facilities>
<Available>True</Available>
</Hotel>
<Hotel>
<ID>2</ID>
<Name>Golden Tulip Little Palace</Name>
<Star>4</Star>
<Facilities>
<facility1>Internet</facility1>
<facility2>Gym</facility2>
<facility3>Parking</facility3>
<facility4>Restaurant</facility4>
</Facilities>
<Available>False</Available>
</Hotel>
</Paris>
"""
#coverting xml to Python dictionary
dict_data = xmltodict.parse(my_xml)
#coverting to json
json_data = json.dumps(dict_data, indent=2)
print(json_data)
Output
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.
import json
import xmltodict
# opening the xml file
with open("hotels.xml","r") as xmlfileObj:
#converting xml data to dictionary
data_dict = xmltodict.parse(xmlfileObj.read())
xmlfileObj.close()
#creating JSON object using dictionary object
jsonObj= json.dumps(data_dict)
#storing json data to json file
with open("hotels.json", "w") as jsonfileObj:
jsonfileObj.write(jsonObj)
jsonfileObj.close()
Output
The Python interpreter does not show any error; it means that the JSON data is successfully saved in a .json file.
Conclusion
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.
from Linux Hint https://ift.tt/36Cn5RU
0 Comments