Skip to main content

Simple Python Library to convert JSON to XML

Project description

https://badge.fury.io/py/json2xml.svg https://static.pepy.tech/personalized-badge/json2xml?period=total&units=international_system&left_color=blue&right_color=orange&left_text=Downloads https://github.com/vinitkumar/json2xml/actions/workflows/pythonpackage.yml/badge.svg https://img.shields.io/pypi/pyversions/json2xml.svg Documentation Status https://codecov.io/gh/vinitkumar/json2xml/branch/master/graph/badge.svg?token=Yt2h55eTL2

A simple Python Library to convert JSON to XML Documentation: https://json2xml.readthedocs.io.

An earlier dependency dict2xml project has been integrated into the project itself. It helped in cleaning up the code, adding types and tests.

Features

The library supports the following features:

  • convert from a json string

  • convert from a json file

  • convert from an API that emits json data

Usage

The library can be used in these ways:

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 like this:
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 a JSON file like this:
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

Assume the following json input

{
  "my_items": [
    { "my_item": { "id": 1 } },
    { "my_item": { "id": 2 } }
  ],
  "my_str_items": ["a", "b"]
}

By default, items in an array are wrapped in <item></item>.

Default output:

<?xml version="1.0" ?>
<all>
  <my_items type="list">
    <item type="dict">
      <my_item type="dict">
        <id type="int">1</id>
      </my_item>
    </item>
    <item type="dict">
      <my_item type="dict">
        <id type="int">2</id>
      </my_item>
    </item>
  </my_items>
  <my_str_items type="list">
    <item type="str">a</item>
    <item type="str">b</item>
  </my_str_items>
  <empty type="list"/>
</all>

However, you can change this behavior using the item_wrap property 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} }],"my_str_items":["a","b"]}')
print(json2xml.Json2xml(data, item_wrap=False).to_xml())

Outputs this:

<?xml version="1.0" ?>
<all>
  <my_items type="list">
    <my_item type="dict">
      <id type="int">1</id>
    </my_item>
    <my_item type="dict">
      <id type="int">2</id>
    </my_item>
  </my_items>
  <my_str_items type="str">a</my_str_items>
  <my_str_items type="str">b</my_str_items>
</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.

How to run tests

This is provided by pytest, which is straight forward.

virtualenv venv -p $(which python3.9)
pip install -r requirements-dev.txt
python setup.py install
pytest -vv

Help and Support to maintain this project

History

4.0.0 / 2023-07-19

  • feat: use Python 3.12 beta 4

  • feat: check against pypy 3.10 too

  • fix: drop support for Python 3.7

  • Use fixture for attrs with dict and remove comment (#170)

3.21.0 / 2023-01-18

  • Implement list item with attributes (#169)

  • feat: add python3.12 alpha4 to the fix (#168)

  • Feat/replace requests with urllib3 (#167)

  • feat: add security reporting guidelines

  • Add CodeQL workflow for GitHub code scanning (#160)

  • Default is False for list_headers (#164)

  • fix: ci issue due to mypy update on python3.11 (#166)

  • chore(deps): bump certifi from 2021.10.8 to 2022.12.7 in /docs (#162)

  • fix: open files properly

  • feat: 3.11 is released 🎉 (#159)

3.20.0 / 2022-10-16

  • feat: make deps more flexible (#158)

  • fix: use SystemRandom as a secure way for generating Random Integers (#156)

3.19.5 / 2022-09-18

  • fix #138 and #151 + 2 new unit tests (#154)

  • fix non-failing and failing unit tests for #152 (#153)

3.19.4 / 2022-07-24

  • Feat/unittest to pytest (#149)

  • Feat/upgrade python test python311beta (#148)

  • feat: test new version of 3.10 and 3.11 (#147)

3.19.3 / 2022-07-01

  • Update utils.py add encoding type UTF-8 @readfromjson function as language(korean) needs UTF-8 encoding options. (#145)

3.19.2 / 2022-06-09

  • escape xml char when having attrs (#144)

  • feat: adjust pytest config setting for easy loggin

  • feat: bump python 3.10 and 3.11 version (#142)

3.19.1 / 2022-06-05

  • bump version of docs building

  • chore(deps): bump waitress from 2.1.1 to 2.1.2 in /docs (#141)

  • Docs update for dicttoxml (#140)

3.19.0 / 2022-05-20

  • Setting xsi location (#135)

  • Repeat list headers (#138)

  • add python3.11 support (#139)

  • improved docs dicttoxml (#134)

  • Fix/types working check ci mypy (#133)

  • fix: ci supports mypy now (#132)

  • generate changelog

  • Feat/remove logging by default (#131)

  • Merge two dev requirements file (#129)

  • remove old unused config

  • feat: add types (#125)

  • fix: issue with twine check

  • fix: issue with long description

  • refactor: xmltodict is only test dependency (#124)

  • fix: add correct list of contributors

  • feat: generate changelog

  • improvements on dicttoxml (#121)

  • feat: add correct badge

  • feat: use codecov

  • fix: flake8 tests

  • fix: add coverage to the mix

  • fix: lint issues and CI

  • fix: check new CI stuff like lint and coverage

  • feat: bump version and generate changelog

  • fix: issue with wrong output for boolean list

  • fix: pull requests also trigger action runs

3.18.0 / 2022-04-23

  • bump version

  • improvements on dicttoxml (#121)

  • feat: add correct badge

  • feat: use codecov

  • fix: flake8 tests

  • fix: add coverage to the mix

  • fix: lint issues and CI

  • fix: check new CI stuff like lint and coverage

  • feat: bump version and generate changelog

  • fix: issue with wrong output for boolean list

  • fix: pull requests also trigger action runs

3.17.1 / 2022-04-20

  • fix: issue with wrong output for boolean list

  • fix: pull requests also trigger action runs

3.17.0 / 2022-04-18

  • fix: return correct xml type for bool (#119)

  • feat: add download counter

  • fix: check latest alpha (#116)

  • fix: check latest alpha (#115)

  • chore(deps): bump waitress from 2.0.0 to 2.1.1 in /docs (#114)

  • feat: only python3 wheels are created now

3.15.0 / 2022-02-24

  • Merge remote-tracking branch ‘origin/master’

  • bump version and prepare for new release

  • feat: new python versions to test against (#110)

  • Fix/perflint (#109)

  • feat: support latest version of 3.10 and 3.11 alpha3 (#98)

  • feat: generate changelog

  • fix: remove unused imports

  • bump version

  • fix: issue with uncaught UnicodeDecodeError

  • cancel jobs for concurrent builds in same PR

  • pypi is stable now

  • feat: update tox config

v3.14.0 / 2022-02-10

  • fix: remove unused imports

  • bump version

  • fix: issue with uncaught UnicodeDecodeError

  • cancel jobs for concurrent builds in same PR

  • pypi is stable now

  • feat: update tox config

v3.11.0 / 2022-01-31

  • bump version

  • feat: remove comments

  • Feat: install pytest separately and run pytests now

  • fix tox

  • add some documentation on testing

  • split testing libs away from release

  • fix: update changelog

  • bump version to 3.10.0

  • fix: we support Python3.7+ now (#101)

  • Issue: #99 dicttoxml igores the root param (#100)

v3.10.0 / 2022-01-29

  • bump version to 3.10.0

  • fix: we support Python3.7+ now (#101)

  • Issue: #99 dicttoxml igores the root param (#100)

  • feat: bump to a rc1 version

  • Add support for Python3.11 alpha and upgrade pytest and py (#97)

  • Feat: drop 3.11.0 alphas from the test matrix for now

  • feat: find the versions that are in the CI

  • fix: typo in the name of python 3.11 version

  • sunsetting python 3.6 and add support for python3.11 alpha

  • chore: prepare for release 3.9.0

  • fix email

  • fix readme

    • update readme - add tests - refactor

  • resolve #93

  • chore: run black on readme doc

  • fix: more issues

  • fix: garbage in history

  • feat: generate history

v3.9.0 / 2021-12-19

  • feat: generate history

  • feat: item_wrap for str and int (#93)

v3.8.4 / 2021-10-24

  • bump version

  • fix: version bump and readme generator

v3.8.3 / 2021-10-24

  • bump version

  • feat: reproduce the error in the test (#90)

  • Feat/version (#88)

  • Feat/docs theme change (#87)

  • Feat/docs theme change (#86)

  • Feat/docs theme change (#85)

  • Feat/docs theme change (#84)

  • Feat/docs theme change (#83)

  • feat: update the docs theme (#82)

v3.8.0 / 2021-10-07

  • Feat/security improvements (#81)

  • arrow_up:

    feat: python 3.10 released (#79)

v3.7.0 / 2021-09-11

  • bookmark:

    feat: final release for v3.7.0

  • bookmark:

    feat: bump version

v3.7.0beta2 / 2021-09-10

  • Feat/cleanup and deprecation fix (#78)

  • item ommision (#76)

  • Create FUNDING.yml

v3.7.0beta1 / 2021-08-28

  • Feat/fork and update dict2xml (#75)

  • chore(deps-dev): bump pip from 18.1 to 19.2 (#73)

  • Delete .travis.yml

  • chore(deps-dev): bump lxml from 4.6.2 to 4.6.3 (#68)

  • Bump lxml from 4.1.1 to 4.6.2 (#66)

v3.6.0 / 2020-11-12

  • Feat/wip exceptions (#65)

  • Add .deepsource.toml

  • feat: upgrade the actions

  • feat: try & support more os and python versions

  • Update pythonpackage.yml

v3.5.0 / 2020-08-24

  • feat: remove six as dependency as we are python3 only, resolves #60 (#61)

  • feat: update makefile for the correct command

v3.4.1 / 2020-06-10

  • fix: issues with pypi release and bump version

  • Feat/attr type docs (#58)

  • fix: conflicts

  • Feat/attr type docs (#57)

  • Merge github.com:vinitkumar/json2xml

  • Update json2xml.py (#56)

  • Merge github.com:vinitkumar/json2xml

  • feat: fix typo in the readme

v3.3.3 / 2020-02-05

  • Update README.rst

  • fix: issue with pypi uploads

  • fix: version

  • bump version

  • Update pythonpackage.yml

  • Refactor/prospector cleanup (#50)

  • Update pythonpackage.yml

  • Create pythonpackage.yml

  • Update README.rst

  • fix: typo in readme

  • bump version

  • Feature/attribute support (#48)

  • Feature/attribute support (#47)

  • chore: bump version

  • fix: remove print statement in json read because it confuses people

  • fix typo in readme

v3.0.0 / 2019-02-26

  • Fix/coveralls (#43)

  • update coverage report (#42)

  • Merge pull request #41 from vinitkumar/fix/coveralls

  • add python coveralls

  • Merge pull request #40 from vinitkumar/refactor/cookiecutter

  • update coverage

  • add image for coveralls

  • coverage and coveralls integrations

  • try and trigger coveralls too

  • fix code block in readme

  • add doc about custom wrapper

  • try at reducing the dependencies

  • add tests for custom wrappers as well

  • add tests for actualy dict2xml conversion

  • fix: remove missing import

  • fix: code syntax highlight in the readme again

  • fix: code syntax highlight in the readme again

  • fix: code syntax highlight in the readme

  • chore: update readme with code samples

  • test: add testcases for the different utils method

  • remove unused imports

  • check the third method for generating dict from json string too

  • run correct test files

  • fix tests

  • update requirements and setuptools

  • refactor the module into more maintainable code

  • chore: add boilerplate

  • remove all legacy

  • Fix/cleanup (#38)

  • cleanup: remove unused modules (#37)

  • Merge pull request #35 from vinitkumar/improve-structure

  • cleanup

  • one again try to get the build working

  • travis need full version for latest supported python

  • do not hardcode version in a series

  • update grammar

  • fix conflicts

  • Update LICENSE

  • cleanup readme

  • remove cli

  • some cleanup and update the tests

  • Update readme.md

  • Cleanup Readme.md

  • Update issue templates

  • fix vulnerabilities in requests

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

Uploaded Source

Built Distribution

json2xml-4.0.0-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: json2xml-4.0.0.tar.gz
  • Upload date:
  • Size: 28.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for json2xml-4.0.0.tar.gz
Algorithm Hash digest
SHA256 121117fc78e1892ac264e6813c49c822a84f73a580b07fbd5cc5c238ddab21cf
MD5 c872ce2a0bd2f9cac154301aa2d5de94
BLAKE2b-256 3754b1acfff5831e10f807ebb2ef5b523175274018cf0c9c5c8c78a3650ce752

See more details on using hashes here.

File details

Details for the file json2xml-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: json2xml-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for json2xml-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6c4d6b222b913d9b63b5b391477b478b1c38d3c81f2e2b4f27dbb3d1a391346a
MD5 5e8900cb5df6ca05f41299e5d117b258
BLAKE2b-256 69ded02e58e550f6145d13b7ee4aabd70a3e6bc3ece4031a3d2c8b7c5d00b4ec

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