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
July 16, 2022 - 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.
Discussions

Converting XML to JSON using Python? - Stack Overflow
I've seen a fair share of ungainly XML->JSON code on the web, and having interacted with Stack's users for a bit, I'm convinced that this crowd can help more than the first few pages of Google resu... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to convert XML to JSON using python? - Stack Overflow
I have below XML and I have saved in the file called movies.xml. I need to convert to JSON with some values only. For direct conversion I can use xmltodict. I am using etree and etree.XMLParser().... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Convert XML to Json
if you can at least use the standard python libraries you can parse the xml with the builtin ElementTree , it's not as good as lxml but it should work More on reddit.com
๐ŸŒ r/sysadmin
6
1
September 6, 2024
It's 2025. What's your favorite module or method for converting xml to json and vice versa?
I hate working with XML. I recently had to parse some responses from namecheap and just used ElementTree (xml.etree.ElementTree). I didn't find any recent XML to JSON modules, and my need was minimal, so just stuck to ET. I would hate to have to work with XML regularly! More on reddit.com
๐ŸŒ r/learnpython
12
6
March 18, 2025
๐ŸŒ
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
๐ŸŒ
Quora
quora.com โ€บ How-can-I-convert-XML-to-JSON-in-Python
How to convert XML to JSON in Python - Quora
STEP 1: install xmltodict module using pip or any other python package manager ... 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.
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.

๐ŸŒ
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.
๐ŸŒ
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

๐ŸŒ
Linux Hint
linuxhint.com โ€บ python_xml_to_json
Python XML to JSON
March 21, 2024 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
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
๐ŸŒ
Sonra
sonra.io โ€บ home โ€บ xml โ€บ xml conversion using python in 2025
XML Conversion Using Python in 2026 - Sonra
1 week ago - 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.
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

๐ŸŒ
Medium
amacal.medium.com โ€บ xml-to-json-in-python-f68637795b07
XML to JSON in Python - Adrian Macal - Medium
November 19, 2020 - 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 XML files to JSON rows in pure Python.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ parsing xml file with beautifulsoup
r/learnpython on Reddit: parsing XML file with beautifulsoup
February 7, 2023 -

A sample of the xml file I'm trying to parse an search for elements looks like this:

                  <c04 level="file">
                     <did>
                        <unittitle id="mss85579_042_076_2" encodinganalog="245$a"> Washington
                           Advisory Group, <date> 1997-2002 </date>
                           <ref target="sub_WAG">
                              <emph render="italic"> See also Container 62</emph></ref>
                        </unittitle>
                        <unitdate encodinganalog="245$f"> 1997-2002 </unitdate>
                        <unitid label="Digital ID">mss85579_042_076</unitid>
                     </did>
                  </c04>
               </c03>
               <c03 level="file">
                  <did>
                     <unittitle encodinganalog="245$a"> Contacts list, <date> 1991-1997 </date>
                     </unittitle>
                     <unitdate encodinganalog="245$f"> 1991-1997 </unitdate>
                     <unitid label="Digital ID">mss85579_042_064</unitid>
                     <unitid label="Digital ID">mss85579_042_106</unitid>
                     <unitid label="Digital ID">mss85579_042_131</unitid>
                  </did>
               </c03>

One of the things I'm trying to do is to search each "did" element for a "unitid" element with the attribute label="Digital ID".

The code I created to do this looks like:

with open(r'C:\Users\Chad\Documents\GitHub\Python_Vir_Env\Python_3.8\XML_Project\Practice_code\convert_test_ms018036.xml', 'r', encoding="latin-1") as file:
    contents = file.read()
    soup = bs(contents, features='lxml-xml')
    all_dids = soup.find_all("did")

    for dids in all_dids():
        unitids = dids.find_all('unitid', {'label':'Digital ID'})
        print(unitids)

If I run the scrip without the for loop and print the "all_dids" variable, I'm it prints all the "did" elements, by when I include the for loop to find the "unitid" elements I receive the message: 'ResultSet' object is not callable.

What am a missing or not quite getting right?