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

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


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.4.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distributions

json2xml-3.4.0-py3.7.egg (6.0 kB view details)

Uploaded Source

json2xml-3.4.0-py2.py3-none-any.whl (4.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file json2xml-3.4.0.tar.gz.

File metadata

  • Download URL: json2xml-3.4.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for json2xml-3.4.0.tar.gz
Algorithm Hash digest
SHA256 6033a168e0a5ba175921200a9d2505e0eaa5dde49b112be0fe1889ed6dc4d596
MD5 ed0f8707bcd20f38e17fa4d426c7dc1d
BLAKE2b-256 d91ce8697d2af2de89f41e5be6066ec9d75e0a648258bc7a03d1259f62dde6fe

See more details on using hashes here.

File details

Details for the file json2xml-3.4.0-py3.7.egg.

File metadata

  • Download URL: json2xml-3.4.0-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

Hashes for json2xml-3.4.0-py3.7.egg
Algorithm Hash digest
SHA256 8c872b985d94a48e3b927000ba8a4d16a5f4fcf4a3c869f457ef41fe5971c57e
MD5 a0f8469743d65d51c30dc079e60b778c
BLAKE2b-256 f008002d52080bbec8f3993b1153a34c067c5c5ed487f76325d6daa90ae617ef

See more details on using hashes here.

File details

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

File metadata

  • Download URL: json2xml-3.4.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for json2xml-3.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e96acbb64f83d219ef52819b684f52469bea41166339d1da4b39ecc7bbf31271
MD5 8385a556035cce3f3fccb10a0cdeb39d
BLAKE2b-256 2f336296c652a1a60c456de997ab9fd4633871ddee6f098f09daf10858651acf

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