Simple Python Library to convert JSON to XML
Project description
json2xml is a Python library that allows you to convert JSON data into XML format. It’s simple, efficient, and easy to use.
Documentation: https://json2xml.readthedocs.io.
The library was initially dependent on the dict2xml project, but it has now been integrated into json2xml itself. This has led to cleaner code, the addition of types and tests, and overall improved performance.
Architecture Diagram
Features
json2xml supports the following features:
Conversion from a json string to XML
Conversion from a json file to XML
Conversion from an API that emits json data to XML
Usage
You can use the json2xml library in the following ways:
from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjson
# Convert JSON data from a URL to XML
data = readfromurl("https://api.publicapis.org/entries")
print(json2xml.Json2xml(data).to_xml())
# Convert a JSON string to XML
data = readfromstring(
'{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}'
)
print(json2xml.Json2xml(data).to_xml())
# Convert a JSON file to XML
data = readfromjson("examples/licht.json")
print(json2xml.Json2xml(data).to_xml())
Custom Wrappers and Indentation
By default, a wrapper all and pretty True is set. However, you can easily change this in your code like this:
from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjson
data = readfromstring(
'{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}'
)
print(json2xml.Json2xml(data, wrapper="all", pretty=True).to_xml())
Outputs this:
<?xml version="1.0" encoding="UTF-8"?>
<all>
<login type="str">mojombo</login>
<id type="int">1</id>
<avatar_url type="str">https://avatars0.githubusercontent.com/u/1?v=4</avatar_url>
</all>
Omit List item
Assume the following json input
{
"my_items": [
{ "my_item": { "id": 1 } },
{ "my_item": { "id": 2 } }
],
"my_str_items": ["a", "b"]
}
By default, items in an array are wrapped in <item></item>.
Default output:
<?xml version="1.0" ?>
<all>
<my_items type="list">
<item type="dict">
<my_item type="dict">
<id type="int">1</id>
</my_item>
</item>
<item type="dict">
<my_item type="dict">
<id type="int">2</id>
</my_item>
</item>
</my_items>
<my_str_items type="list">
<item type="str">a</item>
<item type="str">b</item>
</my_str_items>
<empty type="list"/>
</all>
However, you can change this behavior using the item_wrap property like this:
from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjson
data = readfromstring('{"my_items":[{"my_item":{"id":1} },{"my_item":{"id":2} }],"my_str_items":["a","b"]}')
print(json2xml.Json2xml(data, item_wrap=False).to_xml())
Outputs this:
<?xml version="1.0" ?>
<all>
<my_items type="list">
<my_item type="dict">
<id type="int">1</id>
</my_item>
<my_item type="dict">
<id type="int">2</id>
</my_item>
</my_items>
<my_str_items type="str">a</my_str_items>
<my_str_items type="str">b</my_str_items>
</all>
Optional Attribute Type Support
You can also specify if the output XML needs to have type specified or not. Here is the usage:
from json2xml import json2xml from json2xml.utils import readfromurl, readfromstring, readfromjson data = readfromstring( '{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}' ) print(json2xml.Json2xml(data, wrapper="all", pretty=True, attr_type=False).to_xml())
Outputs this:
<?xml version="1.0" ?>
<all>
<login>mojombo</login>
<id>1</id>
<avatar_url>https://avatars0.githubusercontent.com/u/1?v=4</avatar_url>
</all>
The methods are simple and easy to use and there are also checks inside of code to exit cleanly in case any of the input(file, string or API URL) returns invalid JSON.
Development
This project uses modern Python development practices. Here’s how to set up a development environment:
# Create and activate virtual environment (using uv - recommended) uv venv source .venv/bin/activate # On Windows: .venv\Scripts\activate # Install dependencies uv pip install -r requirements-dev.txt uv pip install -e .
Running Tests and Checks
We provide several ways to run tests and quality checks:
Using Make (recommended):
make test # Run tests with coverage make lint # Run linting with ruff make typecheck # Run type checking with mypy make check-all # Run all checks (lint, typecheck, test)
Using the development script:
python dev.py # Run all checks python dev.py test # Run tests only python dev.py lint # Run linting only python dev.py typecheck # Run type checking only
Using tools directly:
pytest --cov=json2xml --cov-report=term -xvs tests -n auto ruff check json2xml tests mypy json2xml tests
Help and Support to maintain this project
You can sponsor my work for this plugin here: https://github.com/sponsors/vinitkumar/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file json2xml-5.1.0.tar.gz
.
File metadata
- Download URL: json2xml-5.1.0.tar.gz
- Upload date:
- Size: 33.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
cc4c1739d49c59ea582d558bae6a2bfff7d53995f1f6f3837bb9a27b2eb679f6
|
|
MD5 |
2fa6fe2ada63a3540440c5e9de40b38a
|
|
BLAKE2b-256 |
c1b9eaa03e637e4ba77a853c1ec24d929dc39e57dc06786aa2f9325871a2ec39
|
File details
Details for the file json2xml-5.1.0-py3-none-any.whl
.
File metadata
- Download URL: json2xml-5.1.0-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
5715b3b6e8e9de479a8649c2d6f5cb43171c8a2228c5bc66e3dc59158b208dda
|
|
MD5 |
948ac883e573bb126d089b06faf0d0ee
|
|
BLAKE2b-256 |
949dc1632380ebdaca7048c428b0602e13ade625d85470b46e3ce299331ef5c2
|