JSON to XML Converter

6.0.6 · active · verified Thu Apr 16

json2xml is a simple and efficient Python library designed to convert JSON data into XML format. Currently at version 6.0.6, it is actively maintained with a focus on performance, notably through an optional native Rust extension. Its release cadence is regular, addressing bugs, adding features, and enhancing performance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates converting a JSON string to XML using the `json2xml` class and the `readfromstring` utility. It also shows how to specify a custom root wrapper and enable pretty printing. The library automatically uses the faster Rust backend if `json2xml[fast]` was installed.

from json2xml import json2xml
from json2xml.utils import readfromstring

json_data_string = '{"items": [{"id": 1, "name": "Item A"}, {"id": 2, "name": "Item B"}]}'
data = readfromstring(json_data_string)

# Convert JSON to XML with default wrapper 'all' and pretty printing
xml_output = json2xml.Json2xml(data, wrapper="items", pretty=True).to_xml()
print(xml_output)

# Example with the fast Rust backend (if installed via pip install json2xml[fast])
# from json2xml.dicttoxml_fast import dicttoxml_fast
# xml_output_fast = dicttoxml_fast(data, wrapper="items", pretty=True)
# print(xml_output_fast)

view raw JSON →