JSON to XML Converter
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
-
ModuleNotFoundError: No module named 'json2xml'
cause The `json2xml` library is not installed in the current Python environment or the environment is not active.fixRun `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
cause 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.fixValidate 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
cause Older versions or specific configurations might struggle to represent `null` JSON values gracefully in XML, leading to conversion issues.fixUpgrade 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
cause 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.fixRename your Python script file to something different (e.g., `my_converter.py`) to avoid shadowing the library import.
Warnings
- gotcha For 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).
- gotcha Double-escaping of XML attribute values occurred in versions prior to v6.0.5, leading to malformed XML output for certain key names.
- gotcha When 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`.
- gotcha The library expects well-formed JSON input. Providing invalid or malformed JSON can lead to conversion errors or unexpected XML output.
Install
-
pip install json2xml -
pip install json2xml[fast]
Imports
- json2xml
from json2xml import json2xml
- readfromstring
from json2xml.utils import readfromstring
- readfromjson
from json2xml.utils import readfromjson
- readfromurl
from json2xml.utils import readfromurl
Quickstart
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)