Skip to main content

Simple Python Library to convert JSON to XML

Project description

json2xml

https://img.shields.io/pypi/v/json2xml.svg https://img.shields.io/travis/vinitkumar/json2xml.svg Documentation Status Updates https://coveralls.io/repos/github/vinitkumar/json2xml/badge.svg?branch=master

Simple Python Library to convert JSON to XML

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">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

By default, items in an array are wrapped in <item></item>. However, you can change this easily in your code 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} }]}'
)
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">1</id>
    </my_item>
    <my_item>
        <id type="int">2</id>
    </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>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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json2xml-3.7.0b2.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

json2xml-3.7.0b2-py2.py3-none-any.whl (8.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file json2xml-3.7.0b2.tar.gz.

File metadata

  • Download URL: json2xml-3.7.0b2.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.7.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.9.7

File hashes

Hashes for json2xml-3.7.0b2.tar.gz
Algorithm Hash digest
SHA256 f62847e5d7aeffbbb0b49f23df895ee81bd238db91340a99ebfa0d551f7c4f39
MD5 4aac559c3ca473a98b4f2cbf564478b0
BLAKE2b-256 550fa5839ce375c4cfa4935c53a7d019ca85f4d57658fe6cb1222cc934393f16

See more details on using hashes here.

File details

Details for the file json2xml-3.7.0b2-py2.py3-none-any.whl.

File metadata

  • Download URL: json2xml-3.7.0b2-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.7.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.9.7

File hashes

Hashes for json2xml-3.7.0b2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b15354f0a4741fb93879eece00709b69c1035911b05346c668314392ce5cfa1d
MD5 037f21ce0e238d1354244fbad94798e6
BLAKE2b-256 306a461f2ce035150fdf11202abf54f5bf9d5018814f1059ed254b0b494390d3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page