json2xml


Namejson2xml JSON
Version 5.2.0 PyPI version JSON
download
home_pageNone
SummarySimple Python Library to convert JSON to XML
upload_time2025-07-20 19:31:45
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseApache Software License 2.0
keywords json2xml
VCS
bugtrack_url
requirements defusedxml urllib3
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ========
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/json2xml

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



Architecture Diagram
^^^^^^^^^^^^^^^^^^^^

.. image:: ./diagram.png


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 XML

Usage
^^^^^

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

.. code-block:: xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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

.. 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 <item></item>.

Default output:

.. code-block:: xml

    <?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:

.. code-block:: python

    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:

.. code-block:: xml

    <?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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

.. code-block:: xml

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

Development
^^^^^^^^^^^

This project uses modern Python development practices. Here's how to set up a development environment:

 .. code-block:: console

    # Create and activate virtual environment (using uv - recommended)
    uv venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
    
    # Install dependencies
    uv pip install -r requirements-dev.txt
    uv pip install -e .

**Running Tests and Checks**

We provide several ways to run tests and quality checks:

Using Make (recommended):

 .. code-block:: console

    make test          # Run tests with coverage
    make lint          # Run linting with ruff
    make typecheck     # Run type checking with mypy
    make check-all     # Run all checks (lint, typecheck, test)

Using the development script:

 .. code-block:: console

    python dev.py          # Run all checks
    python dev.py test     # Run tests only
    python dev.py lint     # Run linting only
    python dev.py typecheck # Run type checking only

Using tools directly:

 .. code-block:: console

    pytest --cov=json2xml --cov-report=term -xvs tests -n auto
    ruff check json2xml tests
    mypy json2xml tests


Help and Support to maintain this project
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

- You can sponsor my work for this plugin here: https://github.com/sponsors/vinitkumar/


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "json2xml",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "json2xml",
    "author": null,
    "author_email": "Vinit Kumar <mail@vinitkumar.me>",
    "download_url": "https://files.pythonhosted.org/packages/30/55/914349e96d080ff727be187952e0aee82629838be931be5ee6c86e28e253/json2xml-5.2.0.tar.gz",
    "platform": null,
    "description": "========\njson2xml\n========\n\n.. image:: https://badge.fury.io/py/json2xml.svg\n.. image:: https://static.pepy.tech/personalized-badge/json2xml?period=total&units=international_system&left_color=blue&right_color=orange&left_text=Downloads\n        :target: https://pepy.tech/project/json2xml\n\n.. image:: https://github.com/vinitkumar/json2xml/actions/workflows/pythonpackage.yml/badge.svg\n.. image:: https://img.shields.io/pypi/pyversions/json2xml.svg\n.. image:: https://readthedocs.org/projects/json2xml/badge/?version=latest\n        :target: https://json2xml.readthedocs.io/en/latest/?badge=latest\n        :alt: Documentation Status\n.. image:: https://codecov.io/gh/vinitkumar/json2xml/branch/master/graph/badge.svg?token=Yt2h55eTL2\n      :target: https://codecov.io/gh/vinitkumar/json2xml\n\njson2xml is a Python library that allows you to convert JSON data into XML format. It's simple, efficient, and easy to use.\n\nDocumentation: https://json2xml.readthedocs.io.\n\nThe 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.\n\n\n\nArchitecture Diagram\n^^^^^^^^^^^^^^^^^^^^\n\n.. image:: ./diagram.png\n\n\nFeatures\n^^^^^^^^\n\njson2xml supports the following features:\n\n* Conversion from a `json` string to XML\n* Conversion from a `json` file to XML\n* Conversion from an API that emits `json` data to XML\n\nUsage\n^^^^^\n\nYou can use the json2xml library in the following ways:\n\n\n.. code-block:: python\n\n    from json2xml import json2xml\n    from json2xml.utils import readfromurl, readfromstring, readfromjson\n\n    # Convert JSON data from a URL to XML\n    data = readfromurl(\"https://api.publicapis.org/entries\")\n    print(json2xml.Json2xml(data).to_xml())\n\n    # Convert a JSON string to XML\n    data = readfromstring(\n        '{\"login\":\"mojombo\",\"id\":1,\"avatar_url\":\"https://avatars0.githubusercontent.com/u/1?v=4\"}'\n    )\n    print(json2xml.Json2xml(data).to_xml())\n\n    # Convert a JSON file to XML\n    data = readfromjson(\"examples/licht.json\")\n    print(json2xml.Json2xml(data).to_xml())\n\n\nCustom Wrappers and Indentation\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nBy default, a wrapper `all` and pretty `True` is set. However, you can easily change this in your code like this:\n\n.. code-block:: python\n\n    from json2xml import json2xml\n    from json2xml.utils import readfromurl, readfromstring, readfromjson\n\n    data = readfromstring(\n        '{\"login\":\"mojombo\",\"id\":1,\"avatar_url\":\"https://avatars0.githubusercontent.com/u/1?v=4\"}'\n    )\n    print(json2xml.Json2xml(data, wrapper=\"all\", pretty=True).to_xml())\n\n\nOutputs this:\n\n.. code-block:: xml\n\n    <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n    <all>\n      <login type=\"str\">mojombo</login>\n      <id type=\"int\">1</id>\n      <avatar_url type=\"str\">https://avatars0.githubusercontent.com/u/1?v=4</avatar_url>\n    </all>\n\nOmit List item\n^^^^^^^^^^^^^^\n\nAssume the following json input\n\n.. code-block:: json\n\n    {\n      \"my_items\": [\n        { \"my_item\": { \"id\": 1 } },\n        { \"my_item\": { \"id\": 2 } }\n      ],\n      \"my_str_items\": [\"a\", \"b\"]\n    }\n\nBy default, items in an array are wrapped in <item></item>.\n\nDefault output:\n\n.. code-block:: xml\n\n    <?xml version=\"1.0\" ?>\n    <all>\n      <my_items type=\"list\">\n        <item type=\"dict\">\n          <my_item type=\"dict\">\n            <id type=\"int\">1</id>\n          </my_item>\n        </item>\n        <item type=\"dict\">\n          <my_item type=\"dict\">\n            <id type=\"int\">2</id>\n          </my_item>\n        </item>\n      </my_items>\n      <my_str_items type=\"list\">\n        <item type=\"str\">a</item>\n        <item type=\"str\">b</item>\n      </my_str_items>\n      <empty type=\"list\"/>\n    </all>\n\nHowever, you can change this behavior using the item_wrap property like this:\n\n.. code-block:: python\n\n    from json2xml import json2xml\n    from json2xml.utils import readfromurl, readfromstring, readfromjson\n\n    data = readfromstring('{\"my_items\":[{\"my_item\":{\"id\":1} },{\"my_item\":{\"id\":2} }],\"my_str_items\":[\"a\",\"b\"]}')\n    print(json2xml.Json2xml(data, item_wrap=False).to_xml())\n\nOutputs this:\n\n.. code-block:: xml\n\n    <?xml version=\"1.0\" ?>\n    <all>\n      <my_items type=\"list\">\n        <my_item type=\"dict\">\n          <id type=\"int\">1</id>\n        </my_item>\n        <my_item type=\"dict\">\n          <id type=\"int\">2</id>\n        </my_item>\n      </my_items>\n      <my_str_items type=\"str\">a</my_str_items>\n      <my_str_items type=\"str\">b</my_str_items>\n    </all>\n\nOptional Attribute Type Support\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nYou can also specify if the output XML needs to have type specified or not. Here is the usage:\n\n .. code-block:: python\n\n    from json2xml import json2xml\n    from json2xml.utils import readfromurl, readfromstring, readfromjson\n\n    data = readfromstring(\n        '{\"login\":\"mojombo\",\"id\":1,\"avatar_url\":\"https://avatars0.githubusercontent.com/u/1?v=4\"}'\n    )\n    print(json2xml.Json2xml(data, wrapper=\"all\", pretty=True, attr_type=False).to_xml())\n\n\nOutputs this:\n\n.. code-block:: xml\n\n    <?xml version=\"1.0\" ?>\n    <all>\n      <login>mojombo</login>\n      <id>1</id>\n      <avatar_url>https://avatars0.githubusercontent.com/u/1?v=4</avatar_url>\n    </all>\n\n\nThe methods are simple and easy to use and there are also checks inside of code to exit cleanly\nin case any of the input(file, string or API URL) returns invalid JSON.\n\nDevelopment\n^^^^^^^^^^^\n\nThis project uses modern Python development practices. Here's how to set up a development environment:\n\n .. code-block:: console\n\n    # Create and activate virtual environment (using uv - recommended)\n    uv venv\n    source .venv/bin/activate  # On Windows: .venv\\Scripts\\activate\n    \n    # Install dependencies\n    uv pip install -r requirements-dev.txt\n    uv pip install -e .\n\n**Running Tests and Checks**\n\nWe provide several ways to run tests and quality checks:\n\nUsing Make (recommended):\n\n .. code-block:: console\n\n    make test          # Run tests with coverage\n    make lint          # Run linting with ruff\n    make typecheck     # Run type checking with mypy\n    make check-all     # Run all checks (lint, typecheck, test)\n\nUsing the development script:\n\n .. code-block:: console\n\n    python dev.py          # Run all checks\n    python dev.py test     # Run tests only\n    python dev.py lint     # Run linting only\n    python dev.py typecheck # Run type checking only\n\nUsing tools directly:\n\n .. code-block:: console\n\n    pytest --cov=json2xml --cov-report=term -xvs tests -n auto\n    ruff check json2xml tests\n    mypy json2xml tests\n\n\nHelp and Support to maintain this project\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n- You can sponsor my work for this plugin here: https://github.com/sponsors/vinitkumar/\n\n",
    "bugtrack_url": null,
    "license": "Apache Software License 2.0",
    "summary": "Simple Python Library to convert JSON to XML",
    "version": "5.2.0",
    "project_urls": {
        "Homepage": "https://github.com/vinitkumar/json2xml"
    },
    "split_keywords": [
        "json2xml"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "9d9008df9200ffbfe3c6f8e75f8da013348bee31be52e98fde482660b6bb4dc4",
                "md5": "2805a90f4d1a46dcddffd59e1df3b6b0",
                "sha256": "a31b4f2fbf9f78289cbfeaeff1b732341e424ce485e212fd4b7dde623accd548"
            },
            "downloads": -1,
            "filename": "json2xml-5.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2805a90f4d1a46dcddffd59e1df3b6b0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 14545,
            "upload_time": "2025-07-20T19:31:44",
            "upload_time_iso_8601": "2025-07-20T19:31:44.487922Z",
            "url": "https://files.pythonhosted.org/packages/9d/90/08df9200ffbfe3c6f8e75f8da013348bee31be52e98fde482660b6bb4dc4/json2xml-5.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "3055914349e96d080ff727be187952e0aee82629838be931be5ee6c86e28e253",
                "md5": "53356621f67401c99fe890753003e08b",
                "sha256": "280a26a3a19f46f704208c27896eb0a23c661dc61c20e99a8454be78b6be925c"
            },
            "downloads": -1,
            "filename": "json2xml-5.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "53356621f67401c99fe890753003e08b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 36629,
            "upload_time": "2025-07-20T19:31:45",
            "upload_time_iso_8601": "2025-07-20T19:31:45.473487Z",
            "url": "https://files.pythonhosted.org/packages/30/55/914349e96d080ff727be187952e0aee82629838be931be5ee6c86e28e253/json2xml-5.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-20 19:31:45",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "vinitkumar",
    "github_project": "json2xml",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "defusedxml",
            "specs": [
                [
                    "==",
                    "0.7.1"
                ]
            ]
        },
        {
            "name": "urllib3",
            "specs": [
                [
                    "==",
                    "2.5.0"
                ]
            ]
        }
    ],
    "lcname": "json2xml"
}
        
Elapsed time: 1.14830s