Simple Python Library to convert JSON to XML
- Free software: Apache Software License 2.0
- Documentation: https://json2xml.readthedocs.io.
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
The usage is simple:
from json2xml import json2xml, 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 indent 2 is set. However, you can change this easily in your code like this:
from json2xml import json2xml, readfromurl, readfromstring, readfromjson
data = readfromstring(
'{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}'
)
print(json2xml.Json2xml(data, wrapper="custom", indent=8).to_xml())
Outputs this:
<custom>
<avatar_url>https://avatars0.githubusercontent.com/u/1?v=4</avatar_url>
<id>1</id>
<login>mojombo</login>
</custom>
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.
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.