psidney / json2xml

json to xml converter in python3

Home Page:https://json2xml.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

json2xml

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, 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.

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

About

json to xml converter in python3

https://json2xml.readthedocs.io/

License:Other


Languages

Language:HTML 46.7%Language:JavaScript 32.1%Language:CSS 8.9%Language:Python 8.9%Language:Makefile 3.5%