Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vinitkumar/json2xml
json to xml converter in python3
https://github.com/vinitkumar/json2xml
command-line-tool converter json jsontoxml library python ticket tool utility xml
Last synced: 4 days ago
JSON representation
json to xml converter in python3
- Host: GitHub
- URL: https://github.com/vinitkumar/json2xml
- Owner: vinitkumar
- License: other
- Created: 2013-07-06T21:35:57.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2024-12-18T11:45:06.000Z (22 days ago)
- Last Synced: 2024-12-28T15:05:24.190Z (11 days ago)
- Topics: command-line-tool, converter, json, jsontoxml, library, python, ticket, tool, utility, xml
- Language: Python
- Homepage: https://json2xml.readthedocs.io/
- Size: 1.01 MB
- Stars: 100
- Watchers: 2
- Forks: 32
- Open Issues: 8
-
Metadata Files:
- Readme: README.rst
- Changelog: HISTORY.rst
- Contributing: CONTRIBUTING.rst
- Funding: .github/FUNDING.yml
- License: LICENSE
- Security: SECURITY.md
- Authors: AUTHORS.rst
Awesome Lists containing this project
README
========
json2xml
========.. image:: https://badge.fury.io/py/json2xml.svg
.. image:: https://static.pepy.tech/personalized-badge/json2xml?period=total&units=international_system&left_color=blue&right_color=orange&left_text=Downloads
:target: https://pepy.tech/project/json2xml.. image:: https://github.com/vinitkumar/json2xml/actions/workflows/pythonpackage.yml/badge.svg
.. image:: https://img.shields.io/pypi/pyversions/json2xml.svg
.. image:: https://readthedocs.org/projects/json2xml/badge/?version=latest
:target: https://json2xml.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status
.. image:: https://codecov.io/gh/vinitkumar/json2xml/branch/master/graph/badge.svg?token=Yt2h55eTL2
:target: https://codecov.io/gh/vinitkumar/json2xmljson2xml is a Python library that allows you to convert JSON data into XML format. It's simple, efficient, and easy to use.
Documentation: https://json2xml.readthedocs.io.
The library was initially dependent on the `dict2xml` project, but it has now been integrated into json2xml itself. This has led to cleaner code, the addition of types and tests, and overall improved performance.
Features
^^^^^^^^json2xml supports the following features:
* Conversion from a `json` string to XML
* Conversion from a `json` file to XML
* Conversion from an API that emits `json` data to XMLUsage
^^^^^You can use the json2xml library in the following ways:
.. code-block:: python
from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjson# Convert JSON data from a URL to XML
data = readfromurl("https://api.publicapis.org/entries")
print(json2xml.Json2xml(data).to_xml())# Convert a JSON string to XML
data = readfromstring(
'{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}'
)
print(json2xml.Json2xml(data).to_xml())# Convert a JSON file to XML
data = readfromjson("examples/licht.json")
print(json2xml.Json2xml(data).to_xml())Custom Wrappers and Indentation
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^By default, a wrapper `all` and pretty `True` is set. However, you can easily change this in your code like this:
.. code-block:: python
from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjsondata = 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:
.. code-block:: xml
mojombo
1
https://avatars0.githubusercontent.com/u/1?v=4
Omit List item
^^^^^^^^^^^^^^Assume the following json input
.. code-block:: json
{
"my_items": [
{ "my_item": { "id": 1 } },
{ "my_item": { "id": 2 } }
],
"my_str_items": ["a", "b"]
}By default, items in an array are wrapped in .
Default output:
.. code-block:: xml
1
2
a
b
However, you can change this behavior using the item_wrap property like this:
.. code-block:: python
from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjsondata = 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:
.. code-block:: xml
1
2
a
b
Optional Attribute Type Support
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^You can also specify if the output XML needs to have type specified or not. Here is the usage:
.. code-block:: python
from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjsondata = 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:
.. code-block:: xml
mojombo
1
https://avatars0.githubusercontent.com/u/1?v=4
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.
.. code-block:: console
virtualenv venv -p $(which python3.9)
pip install -r requirements-dev.txt
python setup.py install
pytest -vvHelp and Support to maintain this project
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- You can sponsor my work for this plugin here: https://github.com/sponsors/vinitkumar/