The Wayback Machine - https://web.archive.org/web/20240816132323/https://www.geeksforgeeks.org/python-xml-to-json/
Open In App

Python – XML to JSON

Last Updated : 27 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A JSON file is a file that stores simple data structures and objects in JavaScript Object Notation (JSON) format, which is a standard data interchange format. It is primarily used for transmitting data between a web application and a server. A JSON object contains data in the form of a key/value pair. The keys are strings and the values are the JSON types. Keys and values are separated by a colon. Each entry (key/value pair) is separated by a comma. JSON files are lightweight, text-based, human-readable, and can be edited using a text editor.

Note: For more information, refer to Working With JSON Data in Python

XML is a markup language which is designed to store data. It is case sensitive. XML offers you to define markup elements and generate customized markup language. The basic unit in the XML is known as an element. The XML language has no predefined tags. It simplifies data sharing, data transport, platform changes, data availability Extension of an XML file is .xml

Note: For more information, refer to XML | Basics

Both JSON and XML file format are used for transferring data between client and server. 

However, they both serve the same purpose though differ in their own way.

Comparison between JSON and XML

JSON XML
JSON object has a type XML data is typeless
JSON types: string, number, array, Boolean All XML data should be string
Data is readily accessible as JSON objects XML data needs to be parsed
JSON is supported by most browsers Cross-browser XML parsing can be tricky
JSON has no display capabilities XML offers the capability to display data because it is a markup language
JSON supports only text and number data type. XML support various data types such as number, text, images, charts, graphs, etc. It also provides options for transferring the structure or format of the data with actual data.
Retrieving value is easy Retrieving value is difficult
Supported by many Ajax toolkit Not fully supported by Ajax toolkit
A fully automated way of deserializing/serializing JavaScript Developers have to write JavaScript code to serialize/de-serialize from XML
Native support for object The object has to be express by conventions – mostly missed use of attributes and elements.
It supports only UTF-8 encoding. It supports various encoding
It doesn’t support comments. It supports comments.
JSON files are easy to read as compared to XML. XML documents are relatively more difficult to read and interpret.
It does not provide any support for namespaces It supports namespaces.
It is less secured. It is more secure than JSON.

Converting Xml to json

Method 1: Using xmltodict and json module

To handle the JSON file format, Python provides a module named json.

STEP 1: Install xmltodict module using pip or any other python package manager  

pip install xmltodict

STEP 2: import json module using the keyword import 

import json

STEP 3: Read the xml file here, “data_dict” is the variable in which we have loaded our XML data after converting it to dictionary datatype. 

with open("xml_file.xml") as xml_file:
    data_dict = xmltodict.parse(xml_file.read())

STEP 4: Convert the xml_data into a dictionary and store it in a variable JSON object are surrounded by curly braces { }. They are written in key and value pairs.

  • json.loads() takes in a string and returns a json object. 
  • json.dumps() takes in a json object and returns a string. 
  • We use xml_data as input string and generate python object, so we use json.dumps() 
json_data = json.dumps(data_dict)

Here, json_data is the variable used to store the generated object.

STEP 5: Write the json_data to output file 

with open("data.json", "w") as json_file:
        json_file.write(json_data)

Example:
XML File:

Python - XML to JSON

Python – XML to JSON

Python3




# Program to convert an xml
# file to json file
 
# import json module and xmltodict
# module provided by python
import json
import xmltodict
 
 
# open the input xml file and read
# data in form of python dictionary
# using xmltodict module
with open("test.xml") as xml_file:
     
    data_dict = xmltodict.parse(xml_file.read())
    # xml_file.close()
     
    # generate the object using json.dumps()
    # corresponding to json data
     
    json_data = json.dumps(data_dict)
     
    # Write the json data to output
    # json file
    with open("data.json", "w") as json_file:
        json_file.write(json_data)
        # json_file.close()


Output: 

xml to json

xml to json

Method 2:  Using the built-in Python module

XML File

 

Python3




import xml.etree.ElementTree as E
tree = E.parse('a.xml')
root = tree.getroot()
d={}
for child in root:
    if child.tag not in d:
        d[child.tag]=[]
    dic={}
    for child2 in child:
        if child2.tag not in dic:
            dic[child2.tag]=child2.text
    d[child.tag].append(dic)
print(d)


Output:

{
'details': 
[{'firstname': 'Shiv', 'lastname': 'Mishra', 'title': 'Engineer', 'division': 
'Computer', 'building': '301', 'room': '11'},
 {'firstname': 'Yuh', 'lastname': 'Datta', 'title': 'developer', 'division':
  'Computer', 'building': '303', 'room': '02'},
 {'firstname': 'Rahil', 'lastname': 'Khan', 'title': 'Tester', 'division':
  'Computer', 'building': '304', 'room': '10'},
 {'firstname': 'Deep', 'lastname': 'Parekh', 'title': 'Designer', 'division': 
 'Computer', 'building': '305', 'room': '14'}]
}


Previous Article
Next Article

Similar Reads

Python - JSON to XML
A JSON file is a file that stores simple data structures and objects in JavaScript Object Notation (JSON) format, which is a standard data interchange format. It is primarily used for transmitting data between a web application and a server.A JSON object contains data in the form of a key/value pair. The keys are strings and the values are the JSON
5 min read
Python - Difference between json.dump() and json.dumps()
JSON is a lightweight data format for data interchange which can be easily read and written by humans, easily parsed and generated by machines. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called json. Note: For more information, refer to Working With JSON Data in Python json.dumps() json.
2 min read
Python - Difference Between json.load() and json.loads()
JSON (JavaScript Object Notation) is a script (executable) file which is made of text in a programming language, is used to store and transfer the data. It is a language-independent format and is very easy to understand since it is self-describing in nature. Python has a built-in package called json. In this article, we are going to see Json.load a
3 min read
Python program to convert XML to Dictionary
In this article, we will discuss how to convert an XML to a dictionary using Python. Modules Usedxmltodict: It is a Python module that makes working with XML feel like you are working with [JSON]. Run the following command in the terminal to install the module. Syntax: pip install xmltodict pprint: The pprint module provides a capability to “pretty
2 min read
Create XML Documents using Python
Extensible Markup Language(XML), is a markup language that you can use to create your own tags. It was created by the World Wide Web Consortium (W3C) to overcome the limitations of HTML, which is the basis for all Web pages. XML is based on SGML - Standard Generalized Markup Language. It is used for storing and transporting data. XML doesn’t depend
3 min read
How to Parse and Modify XML in Python?
XML stands for Extensible Markup Language. It was designed to store and transport data. It was designed to be both human- and machine-readable. That’s why, the design goals of XML emphasize simplicity, generality, and usability across the Internet. Note: For more information, refer to XML | Basics Here we consider that the XML file is present in th
4 min read
Serialize Python dictionary to XML
XML is a markup language that is designed to transport data. It was made while keeping it self descriptive in mind. Syntax of XML is similar to HTML other than the fact that the tags in XML aren't pre-defined. This allows for data to be stored between custom tags where the tag contains details about the data and the data is stored in between the op
5 min read
Parsing XML with DOM APIs in Python
The Document Object Model (DOM) is a programming interface for HTML and XML(Extensible markup language) documents. It defines the logical structure of documents and the way a document is accessed and manipulated. Parsing XML with DOM APIs in python is pretty simple. For the purpose of example we will create a sample XML document (sample.xml) as bel
2 min read
Modify XML files with Python
Python|Modifying/Parsing XML Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.The design goals of XML focus on simplicity, generality, and usability across the Internet.It is a textual data format with strong support via Unicode for
4 min read
Python IMDbPY – Series Information in XML format
In this article we will see how we can get the company information in the XML format. Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. Series object contains all the information about the all the episodes and seasons that has record
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg