Registry / serialization / json2xml

json2xml

JSON →
library6.3.0pypypi✓ verified 92d ago

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.

pip install json2xml
INSTALL
IMPORT
SIG · JSON2XML
J
json2xml
serializationpythonv6.3.0
Install
1.8s avg
Import
210ms
Disk
18MB
Pass rate
6/ 10
Env Coverage6 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v6.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
2/4 runs
✓ 1.73s
py 3.11
2/4 runs
✓ 1.95s
py 3.12
2/4 runs
✓ 1.65s
py 3.13
2/4 runs
✓ 1.95s
py 3.9
✓ —
✓ 1.93s
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

json2xml
✓ from json2xml import json2xml
readfromstring
✓ from json2xml.utils import readfromstring
Useful for converting JSON strings directly.
readfromjson
✓ from json2xml.utils import readfromjson
For reading JSON data from a local file.
readfromurl
✓ from json2xml.utils import readfromurl
For fetching JSON data from a URL.

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)
json2xml --version
Debug
Known issues
gotchaFor optimal performance, especially with large JSON payloads, install `json2xml` with the `[fast]` extra. Without it, the pure Python implementation will be used, which can be significantly slower (up to 149x).
fix
Use `pip install json2xml[fast]` for installation. The library automatically leverages the Rust backend if available.
affects: <6.0.0 (where Rust backend was introduced), or any version installed without `[fast]`
gotchaDouble-escaping of XML attribute values occurred in versions prior to v6.0.5, leading to malformed XML output for certain key names.
fix
Upgrade to version 6.0.5 or newer: `pip install --upgrade json2xml`.
affects: <6.0.5
gotchaWhen using the Rust backend (via `[fast]` installation), very large integers (exceeding Rust's `i64` range) will be represented as strings in the XML output to prevent `OverflowError`.
fix
Be aware of this behavioral difference for extreme integer values. It's a design choice to ensure compatibility without breaking the conversion.
affects: >=6.0.0 (Rust backend)
gotchaThe library expects well-formed JSON input. Providing invalid or malformed JSON can lead to conversion errors or unexpected XML output.
fix
Ensure your input JSON is valid before passing it to `json2xml`. Use a JSON linter or validator for complex structures.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'json2xml'
The `json2xml` library is not installed in the current Python environment or the environment is not active.
fix
Run `pip install json2xml` (or `pip install json2xml[fast]`) in your terminal. If using a virtual environment, ensure it's activated.
XML Parsing Error: not well-formed
The generated XML is syntactically incorrect, often due to issues like invalid JSON input, special characters not being properly escaped (in older versions), or improper handling of data types.
fix
Validate your input JSON for correctness. Upgrade `json2xml` to the latest version to benefit from bug fixes (e.g., attribute escaping in v6.0.5). Review `json2xml` parameters like `wrapper`, `pretty`, and `attr_type`.
Error converting JSON with null values
Older versions or specific configurations might struggle to represent `null` JSON values gracefully in XML, leading to conversion issues.
fix
Upgrade to the latest `json2xml` version. Consider pre-processing your JSON to handle `null` values explicitly (e.g., remove keys with `null` values or replace `null` with empty strings) if issues persist.
Importing json2xml library gives invalid syntax error
This can occur if the Python script file you are running has the same name as the library (e.g., `json2xml.py`), causing a conflict with the installed package.
fix
Rename your Python script file to something different (e.g., `my_converter.py`) to avoid shadowing the library import.
Upgrade
Version history
6.3.0latest on PyPI · released Jun 10, 2026
Audit
Dependencies
json2xml-rsoptionalProvides a native Rust extension for significantly faster JSON to XML conversion (up to 149x). Automatically installed with 'json2xml[fast]'.
Agent activity
15 hits · last 30 days
node
14
Resources
json2xml — pip install json2xml · libregistry