Skip to content
Related Articles

Related Articles

Python – XML to JSON
  • Last Updated : 21 Apr, 2020

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 on way.



Comparison between JSON and XML

JSONXML
JSON object has a typeXML data is typeless
JSON types: string, number, array, BooleanAll XML data should be string
Data is readily accessible as JSON objectsXML data needs to be parsed
JSON is supported by most browsersCross-browser XML parsing can be tricky
JSON has no display capabilitiesXML 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 easyRetrieving value is difficult
Supported by many Ajax toolkitNot fully supported by Ajax toolkit
A fully automated way of deserializing/serializing JavaScriptDevelopers have to write JavaScript code to serialize/de-serialize from XML
Native support for objectThe 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 namespacesIt supports namespaces.
It is less secured.It is more secure than JSON.

Converting Xml to json

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: Close the XML file

xml_file.close()

STEP 5: 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 pyhon 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 6: Write the json_data to output file



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

STEP 7: Close the output file

json_file.close()

Example:

XML File:

python-xml-to-json




# 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:

python-xml-to-json

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :