Nothing came back right away, so I went ahead and wrote a script that solves this problem.

Python already allows you to convert from JSON into a native dict (using json or, in versions < 2.6, simplejson), so I wrote a library that converts native dicts into an XML string.

https://github.com/quandyfactory/dict2xml

It supports int, float, boolean, string (and unicode), array and dict data types and arbitrary nesting (yay recursion).

Answer from Ryan McGreal on Stack Overflow
🌐
PyPI
pypi.org › project › json2xml
json2xml
January 29, 2026 - JavaScript is disabled in your browser. Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
Discussions

How to convert XML to JSON in Python? - Stack Overflow
Possible Duplicate: Converting XML to JSON using Python? I'm doing some work on App Engine and I need to convert an XML document being retrieved from a remote server into an equivalent JSON obj... More on stackoverflow.com
🌐 stackoverflow.com
A simple python3 library to convert JSON into XML via file, URL or string.
So basically dicttoxml.dicttoxml(json.loads(...)) ? More on reddit.com
🌐 r/Python
21
54
November 13, 2017
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
Convert XML to Json natively?

If you are willing to learn a bit of python, this site walks you through what’s needed. I believe everything needed is distributed by RH. https://www.digitalocean.com/community/tutorials/python-xml-to-json-dict

More on reddit.com
🌐 r/redhat
1
1
May 31, 2023
🌐
PyPI
pypi.org › project › xmltojson
xmltojson
JavaScript is disabled in your browser. Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-json-to-xml
Python - JSON to XML - GeeksforGeeks
July 12, 2025 - # Program to read JSON file # and generate its XML file # Importing json module and xml # module provided by python import json as JS import xml.etree.ElementTree as ET # Opening JSON file in read mode with open("myfile3.json", "r") as json_file: # loading json file data # to variable data data = JS.load(json_file); # Building the root element # of the xml file root = ET.Element("quiz") # Building the sub root elements # We don't add text since the value # associated with subelement is a # python dictionary Maths = ET.SubElement(root, "maths") # Building subelement of maths as q1 Q1 = ET.SubElement(Maths, "q1") ET.SubElement(Q1, "question").
🌐
Aspose
products.aspose.com › aspose.cells › python via java › conversion › json to xml
Python JSON to XML - JSON to XML Converter | products.aspose.com
November 13, 2025 - Aspose Excel. This comprehensive solution provides Python developers with a fully integrated approach to convert JSON to XML format, enabling seamless saving of JSON data into XML format using the Aspose.Cells library, all through efficient and customizable Python code.
🌐
Readthedocs
dicttoxml.readthedocs.io › en › latest › user › advanced.html
JSON to XML - dicttoxml2 2.1.0 documentation - Read the Docs
You can easily syntax-check and pretty-print your XML using Python’s xml.dom.minidom module. ... >>> from xml.dom.minidom import parseString >>> dom = parseString(xml) >>> print(dom.toprettyxml()) <?xml version="1.0" ?> <root> <mylist type="list"> <item type="str">foo</item> <item type="str">bar</item> <item type="str">baz</item> </mylist> <mydict type="dict"> <foo type="str">bar</foo> <baz type="int">1</baz> </mydict> <ok type="bool">true</ok> </root>
Find elsewhere
🌐
PythonForBeginners.com
pythonforbeginners.com › home › convert json to xml in python
Convert JSON to XML in Python - PythonForBeginners.com
February 20, 2023 - Once we get the file pointer, we will save the python dictionary to an XML file using the unparse() method defined in the xmltodict module. The unparse() method takes the dictionary as its first argument and the file pointer as the argument ...
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

🌐
GitHub
github.com › simongle › json-to-xml-python
GitHub - simongle/json-to-xml-python: A simple python script using the json and lxml libraries for converting JSON structured data to XML
A simple python script using the json and lxml libraries for converting JSON structured data to XML - simongle/json-to-xml-python
Author   simongle
🌐
Herongyang
herongyang.com › XML › JSON-to-XML-Conversion-Module-for-Java.html
JSON-to-XML Conversion Module for Python
If you are familiar with Python language, you can try to use the "xmltodict" module to convert your JSON document to an XML document.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-xml-to-json
Python - XML to JSON - GeeksforGeeks
July 12, 2025 - # 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()
🌐
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 - Converting complex XML structures to JSON can be challenging due to nested elements and attributes. However, with the right tools and careful handling, it is possible to achieve a seamless conversion. Tools like ‘xmltodict’ in Python can help manage these complexities by correctly mapping XML elements to JSON objects and arrays.
🌐
Python Forum
python-forum.io › thread-2367.html
converting json to xml
HI, I am new to python and learning , need help converting json file to xml . I have below code ( found on web ) modified a bit but is not parsing the whole file only few lines: appreciate any help! **cry** Thanks! Output: Output: &l...
🌐
Reddit
reddit.com › r/python › a simple python3 library to convert json into xml via file, url or string.
r/Python on Reddit: A simple python3 library to convert JSON into XML via file, URL or string.
November 13, 2017 - certifi (2017.11.5) chardet (3.0.4) dict2xml (1.5) idna (2.6) json2xml (2.1.0) lxml (4.1.1) pip (9.0.1) requests (2.18.4) setuptools (36.7.2) six (1.11.0) urllib3 (1.22) wheel (0.30.0) xmltodict (0.11.0) which is waaaaaaaay to much. There are a lot of things that exist in the standard library (such as xml writing/parsing and http requests). Please don't turn Python and pip into nodejs and npm.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-xml-to-json-dict
Python XML to JSON, XML to Dict | DigitalOcean
August 3, 2022 - If we try to unparse this form of JSON, we will face this error: This happens because xmltodict needs to construct the JSON with the very first key as the root XML tag. This means that there should only be a single JSON key at the root level of data. In this lesson, we studied an excellent Python module which can be used to parse and convert XML to JSON and vice-versa.
🌐
Aspose
products.aspose.cloud › aspose.cells › python › conversion › json to xml conversion
Convert JSON to XML using Python - Aspose Cloud
February 5, 2023 - This Cloud SDK empowers Python developers with powerful functionality and ensures high-quality XML output. # For complete examples and data files, please go to https://github.com/aspose-cells-cloud/aspose-cells-cloud-python/ import os import shutil from asposecellscloud.apis.cells_api import CellsApi cells_api = CellsApi(os.getenv('ProductClientId'),os.getenv('ProductClientSecret')) file1 = cells_api.cells_workbook_put_convert_workbook("Book1.json",format="xml") shutil.move(file1, "destFile.xml")
🌐
Medium
cloudmersive.medium.com › how-to-convert-json-to-xml-in-python-aeb05178ef66
How to Convert JSON to XML in Python | by Cloudmersive | Medium
March 18, 2024 - from __future__ import print_function import time import cloudmersive_convert_api_client from cloudmersive_convert_api_client.rest import ApiException from pprint import pprint # Configure API key authorization: Apikey configuration = cloudmersive_convert_api_client.Configuration() configuration.api_key['Apikey'] = 'YOUR_API_KEY' # create an instance of the API class api_instance = cloudmersive_convert_api_client.ConvertDataApi(cloudmersive_convert_api_client.ApiClient(configuration)) json_string = 'json_string_example' # str | Input JSON String to convert to XML try: # Convert JSON String to XML conversion api_response = api_instance.convert_data_json_string_to_xml(json_string) pprint(api_response) except ApiException as e: print("Exception when calling ConvertDataApi->convert_data_json_string_to_xml: %s\n" % e)