preamble
Today we will learn how to convert XML to JSON and XML to dictionary in Python. We can use Python'sxmltodict
module to read XML files and convert them to dictionaries or JSON data. We can also stream on large XML files and convert them to dictionaries. Before we get into the coding part, let's first understand why XML conversion is needed.
Convert XML to Dictionary/JSON
XML files are becoming obsolete, but there are still many large systems on the web that use this format. XML is heavier than JSON, so most developers prefer to use the latter in their applications. Converting XML to JSON can be a tedious task when the application needs to understand the XML provided by any source.xmltodict
module makes this task very simple and intuitive.
Getting started with xmltodict
We can start usingxmltodict
module, but first you need to install it. We will mainly use pip to install it.
Install the xmltodict module
Here's how we install the xmltodict module using the Python Package Index (pip):
pip install xmltodict
due toxmltodict
is a very lightweight module, so installation will be fast. Here is the output of this installation: !python install xmltodict module The best thing is that this module does not depend on any other external modules, so it is lightweight and avoids any version conflicts. For demonstration purposes only, on Debian-based systems, theapt
tool to easily install this module:
sudo apt install python-xmltodict
Another advantage is that the module has an official Debian package.
Python XML to JSON Conversion
The best way to try out this module is to perform one of the operations for which it was originally designed, which is to perform an XML to JSON conversion. Let's take a look at the code snippet that shows how this is done:
import xmltodict import pprint import json my_xml = """ <audience> <id what="attribute">123</id> <name>Shubham</name> </audience> """ pp = (indent=4) (((my_xml)))
Let's take a look at the output of this program: !python xml to json Here, we simply use theparse(...)
function converts the XML data to JSON, and then we use thejson
module to print JSON in a better format.
Convert XML files to JSON
It is neither always possible nor practical to put XML data directly in the code. Usually, we save the data in a database or in some file. We can also just select the file and convert it to JSON. let's see a code snippet on how to perform the conversion using an XML file:
import xmltodict import pprint import json with open('') as fd: doc = (()) pp = (indent=4) ((doc))
Let's look at the output of this program: !python xml file to json Here, we use another module pprint to print the output in a formatted way. In addition to this, the output of the program is formatted using theopen(...)
function is very intuitive, we use it to get the file descriptor and then parse the file into a JSON object.
Python XML to Dictionary Conversion
As the module name itself suggests, xmltodict actually converts the XML data we provide into a simple Python dictionary. As a result, we can also access the data simply using dictionary keys. Here is a sample program:
import xmltodict import pprint import json my_xml = """ <audience> <id what="attribute">123</id> <name>Shubham</name> </audience> """ my_dict = (my_xml) print(my_dict['audience']['id']) print(my_dict['audience']['id']['@what'])
Let's look at the output of this program: !python xml to dict Thus, tags can be used as keys, as well as attribute keys@
symbols as prefixes.
Support for namespaces in XML
In XML data, there is usually a set of namespaces that define the scope of the data provided by the XML file. When converting to JSON format, it is necessary to leave these namespaces unchanged in the JSON format. Let's consider this example XML file:
<root xmlns="/" xmlns:a="/"> <audience> <id what="attribute">123</id> <name>Shubham</name> </audience> </root>
The following is a sample program that demonstrates how to include XML namespaces in a JSON format:
import xmltodict import pprint import json with open('') as fd: doc = ((), process_namespaces=True) pp = (indent=4) ((doc))
Let's look at the output of this program: !xml namespace to dict and json
JSON to XML Conversion
Although converting XML to JSON is the main goal of this module, xmltodict also supports performing the opposite operation, converting JSON to XML format. We will provide JSON data in our program. Below is a sample program:
import xmltodict student = { "data" : { "name" : "Shubham", "marks" : { "math" : 92, "english" : 99 }, "id" : "s387hs3" } } print((student, pretty=True))
Let's take a look at the output of this program:!python json to xml Note that a single JSON key must be provided in order for this to work properly. If we consider modifying our program so that it contains multiple JSON keys at the first level of the data, as shown below:
import xmltodict student = { "name" : "Shubham", "marks" : { "math" : 92, "english" : 99 }, "id" : "s387hs3" } print((student, pretty=True))
In this case, we have three keys at the root level. If we try to parse this form of JSON, we will encounter the following error: !python json to xml unparse error. this is because xmltodict needs to construct the XML using the first key as the root XML tag. This means that there can only be one JSON key at the root level of the data.
reach a verdict
In this course, we learned about an excellent Python module that can be used to parse and convert XML to JSON and vice versa. We also learned how to convert XML to a dictionary using the xmltodict module.
Points to note
1. Read the xml file, if it contains Chinese, to specify the encoding format
with open(‘', encoding=“UTF-8”) as xml_file
2. Convert the dictionary type to a string in json format, you need to turn off the ascii code auto-recognition, otherwise the following results will appear
json_conversion = (parsed_data, ensure_ascii=False)
3. will write the json string into the file, you need to specify the encoding, otherwise in the Chinese place will appear garbled
with open(‘', ‘w', encoding=“UTF-8”) as json_file
summarize
to this article on the Python XML to JSON, XML to dictionary article is introduced to this, more related Python XML to JSON, dictionary content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!