to extract RelatedTerms first you have to extract top Term element using btree.select('Terms > Term') now you can loop it and extract Term inside RelatedTerms using term.select('RelatedTerms > Term')

import json
from bs4 import BeautifulSoup

xml_file = './xml.xml'
btree = BeautifulSoup(open(xml_file, 'r'), "xml")
Terms = btree.select('Terms > Term')
jsonObj = {"thesaurus": []}

for term in Terms:
    termDetail = {
        "Description": term.find('Description').text,
        "Title": term.find('Title').text
    }
    RelatedTerms = term.select('RelatedTerms > Term')
    if RelatedTerms:
        termDetail["RelatedTerms"] = []
        for rterm in RelatedTerms:
            termDetail["RelatedTerms"].append({
                "Title": rterm.find('Title').text,
                "Relationship": rterm.find('Relationship').text
            })
    jsonObj["thesaurus"].append(termDetail)

print json.dumps(jsonObj, indent=4)
Answer from ewwink on Stack Overflow
๐ŸŒ
Finxter
blog.finxter.com โ€บ python-beautifulsoup-xml-to-dict-json-dataframe-csv
Python BeautifulSoup XML to Dict, JSON, DataFrame, CSV โ€“ Be on the Right Side of Change
Again, you will see the use of xmltodict. Because of their similarities, first, convert the file to a dictionary and then later write it to a JSON file. The json_dumps() function is used to take in the XML data.
๐ŸŒ
GitHub
github.com โ€บ arjanski โ€บ tei2json
GitHub - arjanski/tei2json: ๐Ÿ“ Batch transform TEI XML files to JSON (and CSV) using BeautifulSoup and Pandas
๐Ÿ“ Batch transform TEI XML files to JSON (and CSV) using BeautifulSoup, edit with Pandas for further (data science) processing, then serve output via REST API using FastAPI.
Author ย  arjanski
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-xml-to-json
Python - XML to JSON - GeeksforGeeks
April 28, 2025 - After creating a soup of the page if we want to navigate nested tag then with the help of. we can do it. For scraping Nested Tag using Beautifulsoup follow the below-mentioned steps. Step-by-step Approach Step 1: The first s ... Beautiful Soup is a python library used for extracting html and xml files.
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to convert XML to JSON in Python โ€“ Step by Step guide - YouTube
Check out https://www.hellocodeclub.com for more tutorials and projectsLearn how you can convert an XML to JSON in Python and the different python modules av...
Published ย  May 8, 2021
๐ŸŒ
Lee JaeKyu
9033.github.io โ€บ text โ€บ xml2json.html
xml to json | Lee JaeKyu
# python 3 from bs4 import ... ret[child.name].append(t(child)) else: ret[child.name]=t(child) return ret def xmltojson(xml): soup=BeautifulSoup(xml,'html.parser') return json.dumps(souptodict(soup),indent=2)...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-xml-to-json-dict
Python XML to JSON, XML to Dict | DigitalOcean
August 3, 2022 - XML is heavier than JSON and so, most developers prefer the latter in their applications. When applications need to understand the XML provided by any source, it can be a tedious task to convert it to JSON. The xmltodict module in Python makes this task extremely easy and straightforward to perform.
Find elsewhere
๐ŸŒ
PythonForBeginners.com
pythonforbeginners.com โ€บ home โ€บ convert xml to json in python
Convert XML to JSON in Python - PythonForBeginners.com
February 24, 2023 - The parse() method takes an XML string as its input argument and returns the corresponding dictionary. Next, we will convert the python dictionary to a JSON string. For this, we will use the dumps() method defined in the json module.
Top answer
1 of 1
5

I would recommend to use XSLT to transform the XML to JSON:

import json

from lxml import etree

XSL = '''<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="text"/>

    <xsl:template match="/collection">
        <xsl:text>{</xsl:text>
            <xsl:apply-templates/>
        <xsl:text>}</xsl:text>
    </xsl:template>

    <xsl:template match="genre">
        <xsl:text>"</xsl:text>
            <xsl:value-of select="@category"/>
        <xsl:text>": [</xsl:text>
        <xsl:for-each select="descendant::movie" >
            <xsl:text>"</xsl:text>
                <xsl:value-of select="@title"/>
            <xsl:text>"</xsl:text>
            <xsl:if test="position() != last()">
                <xsl:text>, </xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>]</xsl:text>
        <xsl:if test="following-sibling::*">
            <xsl:text>,
</xsl:text>
        </xsl:if>
    </xsl:template>

    <xsl:template match="text()"/>
</xsl:stylesheet>'''

# load input
dom = etree.parse('movies.xml')
# load XSLT
transform = etree.XSLT(etree.fromstring(XSL))

# apply XSLT on loaded dom
json_text = str(transform(dom))

# json_text contains the data converted to JSON format.
# you can use it with the JSON API. Example:
data = json.loads(json_text)
print(data)

Output:

{'Action': ['Indiana Jones: The raiders of the lost Ark', 'THE KARATE KID', 'Back 2 the Future', 'X-Men', 'Batman Returns', 'Reservoir Dogs'], 'Thriller': ['ALIEN', "Ferris Bueller's Day Off", 'American Psycho']}

I don't understand what you want to achieve with "second output" and "third output", though, as these outputs seem to be constants.

๐ŸŒ
Quora
quora.com โ€บ How-can-I-convert-XML-to-JSON-in-Python
How to convert XML to JSON in Python - Quora
Answer (1 of 13): There is a module called xmltodict. Its parse method will convert the XML data into dictionary data structure which can be JSON-dumped. [code]import xmltodict import json with open('xmlfile.xml') as in_file: xml = in_file.read() with open('jsondata.json', 'w') as out_f...
๐ŸŒ
Linux Hint
linuxhint.com โ€บ python_xml_to_json
Python XML to JSON
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
Reddit
reddit.com โ€บ r/sysadmin โ€บ convert xml to json
r/sysadmin on Reddit: Convert XML to Json
September 6, 2024 -

Im dealing with this problem at work and it's the last step I need to implement for a process that will be automated.

We're government so if there's anything in the offiical RedHat repos that can do this conversion it'd make my life easier but as far as I know there isn't.

The reason I can't use something like yq, or python modules like xmltodict, untangle, pandas, or beautifulsoup is brcause they aren't approved.

I know an easy answer is Apache Daffodil but the documentation on that is WAY over my head. Anyone have suggestions

๐ŸŒ
Webscraping
webscraping.fyi โ€บ lib โ€บ compare โ€บ python-beautifulsoup-vs-python-xmltodict
Comparison of python beautifulsoup vs xmltodict libraries - Web Scraping FYI
February 16, 2023 - Alternatively, it can be used in reverse mode to parse JSON documents using HTML parsing tools like CSS selectors and XPath. It can be installed via pip by running pip install xmltodict command. ... from bs4 import BeautifulSoup # this is our HTML page: html = """ <head> <title>Hello World!</title> </head> <body> <div id="product"> <h1>Product Title</h1> <p>paragraph 1</p> <p>paragraph2</p> <span class="price">$10</span> </div> </body> """ soup = BeautifulSoup(html) # we can iterate using dot notation: soup.head.title "Hello World" # or use find method to recursively find matching elements: so
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ xmljson
xmljson ยท PyPI
Converts XML into JSON/Python dicts/arrays and vice-versa.
      ยป pip install xmljson
    
Published ย  Apr 25, 2020
Version ย  0.2.1
Top answer
1 of 7
86

xmltodict (full disclosure: I wrote it) can help you convert your XML to a dict+list+string structure, following this "standard". It is Expat-based, so it's very fast and doesn't need to load the whole XML tree in memory.

Once you have that data structure, you can serialize it to JSON:

import xmltodict, json

o = xmltodict.parse('<e> <a>text</a> <a>text</a> </e>')
json.dumps(o) # '{"e": {"a": ["text", "text"]}}'
2 of 7
27

Soviut's advice for lxml objectify is good. With a specially subclassed simplejson, you can turn an lxml objectify result into json.

import simplejson as json
import lxml

class objectJSONEncoder(json.JSONEncoder):
  """A specialized JSON encoder that can handle simple lxml objectify types
      >>> from lxml import objectify
      >>> obj = objectify.fromstring("<Book><price>1.50</price><author>W. Shakespeare</author></Book>")       
      >>> objectJSONEncoder().encode(obj)
      '{"price": 1.5, "author": "W. Shakespeare"}'       
 """


    def default(self,o):
        if isinstance(o, lxml.objectify.IntElement):
            return int(o)
        if isinstance(o, lxml.objectify.NumberElement) or isinstance(o, lxml.objectify.FloatElement):
            return float(o)
        if isinstance(o, lxml.objectify.ObjectifiedDataElement):
            return str(o)
        if hasattr(o, '__dict__'):
            #For objects with a __dict__, return the encoding of the __dict__
            return o.__dict__
        return json.JSONEncoder.default(self, o)

See the docstring for example of usage, essentially you pass the result of lxml objectify to the encode method of an instance of objectJSONEncoder

Note that Koen's point is very valid here, the solution above only works for simply nested xml and doesn't include the name of root elements. This could be fixed.

I've included this class in a gist here: http://gist.github.com/345559

๐ŸŒ
Sonra
sonra.io โ€บ home โ€บ xml โ€บ xml conversion using python in 2025
XML Conversion Using Python in 2026
January 14, 2025 - This overview introduces key Python libraries, including lxml, ElementTree, xmltodict, BeautifulSoup, and pandas, that facilitate the conversion process. These libraries offer diverse approaches for parsing, modifying, and converting XML.
๐ŸŒ
Integrate.io
integrate.io โ€บ home โ€บ blog โ€บ conversions โ€บ how to convert xml to json: a step-by-step guide
How to Convert XML to JSON: A Step-by-Step Guide | Integrate.io
July 21, 2025 - Popular choices include Python's โ€˜xmltodictโ€™ and โ€˜jsonโ€™ libraries, JavaScript's โ€˜xml2jsโ€™ library, and any online JSON converter like Code Beautify. Select a tool based on your programming environment, the complexity of the XML data, and specific project needs.
๐ŸŒ
Medium
amacal.medium.com โ€บ xml-to-json-in-python-f68637795b07
XML to JSON in Python - Adrian Macal - Medium
November 19, 2020 - XML to JSON in Python Having tested tools like AWS Glue + Athena, EMR and Snowflake in context their performance to query XML files, I decided to give a try very, very naive approach of converting โ€ฆ