json2xml
Simple Python Library to convert JSON to XML
- Free software: Apache Software License 2.0
- Documentation: https://json2xml.readthedocs.io.
Update
The dict2xml project has been forked and integrated in the project itself. This helped with cleaning up the code and also doing improvements. The goal is to remove all the dependencies from this project.
Features
It lets you convert json to xml in following ways:
- from a json string
- from a json file
- from an API that emits json data
Usage
The usage is simple:
from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjson
# get the xml from an URL that return json
data = readfromurl("https://coderwall.com/vinitcool76.json")
print(json2xml.Json2xml(data).to_xml())
# get the xml from a json string
data = readfromstring(
'{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}'
)
print(json2xml.Json2xml(data).to_xml())
# get the data from an URL
data = readfromjson("examples/licht.json")
print(json2xml.Json2xml(data).to_xml())
Custom Wrappers and indent
By default, a wrapper all and pretty True is set. However, you can change this easily 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" ?>
<all>
<login type="str">mojombologin>
<id type="int">1id>
<avatar_url type="str">https://avatars0.githubusercontent.com/u/1?v=4avatar_url>
all>
Omit List item
By default, items in an array are wrapped in
from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjson
data = readfromstring('{"my_items":[{"my_item":{"id":1} },{"my_item":{"id":2} }]}')
print(json2xml.Json2xml(data, item_wrap=False).to_xml())
Outputs this:
xml version="1.0" ?>
<all>
<my_items type="list">
<my_item>
<id type="int">1id>
my_item>
<my_item>
<id type="int">2id>
my_item>
list>
all>
Optional Attribute Type Support
Now, we 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>mojombologin>
<id>1id>
<avatar_url>https://avatars0.githubusercontent.com/u/1?v=4avatar_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.
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.