Simple Python Library to convert JSON to XML
Project description
json2xml
Simple Python Library to convert JSON to XML
Free software: Apache Software License 2.0
Documentation: https://json2xml.readthedocs.io.
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">mojombo</login>
<id type="int">1</id>
<avatar_url type="str">https://avatars0.githubusercontent.com/u/1?v=4</avatar_url>
</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>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.
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
History
3.0.0 (2019-02-22)
First release on PyPI.
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 Distributions
File details
Details for the file json2xml-3.4.1.tar.gz
.
File metadata
- Download URL: json2xml-3.4.1.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00fc721100a2f667bac2f4c90b2c376368b412568219fa65c6c36f6d3b614bfb |
|
MD5 | 51516d0526a906e31d0f4b8f3547530c |
|
BLAKE2b-256 | 81d5b02a85d46c55afb32190db8dceea060e5361ee719278edf867a9ab227cf9 |
File details
Details for the file json2xml-3.4.1-py3.7.egg
.
File metadata
- Download URL: json2xml-3.4.1-py3.7.egg
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1127ab4650224c0e54fa96d94a7035336d8accd0896900c7150f7c2c021a2490 |
|
MD5 | 2cb16b3d48a20eac9b9fc330450f6e26 |
|
BLAKE2b-256 | 5c032aa35bcc345f73dc20d6b5eeeccbf53e70e6c9b08cc315f0cc0f52894fe2 |
File details
Details for the file json2xml-3.4.1-py2.py3-none-any.whl
.
File metadata
- Download URL: json2xml-3.4.1-py2.py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9243e796ef8cf7c5163e7d0c7bc8dd1de52bd8f25b6dc9563d5b9f299c6881f7 |
|
MD5 | 6fa13a89a7d9813e9496adf873333484 |
|
BLAKE2b-256 | cd5996ba255ab3bed49fc47f6f331fa3ce9fca0efab9d9053811396312a9a37f |