{"id":13861888,"url":"https://github.com/vinitkumar/json2xml","last_synced_at":"2026-01-17T02:55:36.425Z","repository":{"id":9368864,"uuid":"11224776","full_name":"vinitkumar/json2xml","owner":"vinitkumar","description":"json to xml converter in python3","archived":false,"fork":false,"pushed_at":"2025-03-30T06:27:00.000Z","size":1117,"stargazers_count":101,"open_issues_count":3,"forks_count":32,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T08:40:55.505Z","etag":null,"topics":["command-line-tool","converter","json","jsontoxml","library","python","ticket","tool","utility","xml"],"latest_commit_sha":null,"homepage":"https://json2xml.readthedocs.io/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vinitkumar.png","metadata":{"files":{"readme":"README.rst","changelog":"HISTORY.rst","contributing":"CONTRIBUTING.rst","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.rst","dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["vinitkumar"]}},"created_at":"2013-07-06T21:35:57.000Z","updated_at":"2025-03-30T06:27:01.000Z","dependencies_parsed_at":"2023-02-15T12:30:46.426Z","dependency_job_id":"42fb6f75-2535-42c3-ba30-27095e92df61","html_url":"https://github.com/vinitkumar/json2xml","commit_stats":{"total_commits":355,"total_committers":17,"mean_commits":20.88235294117647,"dds":0.6366197183098592,"last_synced_commit":"bf6363d0ff01f5c4255e70b8f778ab0c141fa173"},"previous_names":[],"tags_count":49,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinitkumar%2Fjson2xml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinitkumar%2Fjson2xml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinitkumar%2Fjson2xml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinitkumar%2Fjson2xml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vinitkumar","download_url":"https://codeload.github.com/vinitkumar/json2xml/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247809964,"owners_count":20999816,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["command-line-tool","converter","json","jsontoxml","library","python","ticket","tool","utility","xml"],"created_at":"2024-08-05T06:01:32.258Z","updated_at":"2026-01-17T02:55:36.419Z","avatar_url":"https://github.com/vinitkumar.png","language":"Python","funding_links":["https://github.com/sponsors/vinitkumar","https://github.com/sponsors/vinitkumar/"],"categories":["Python"],"sub_categories":[],"readme":".. image:: ./json2xml-hero.png\n   :alt: json2xml hero\n\n\n========\njson2xml\n========\n\n.. image:: https://badge.fury.io/py/json2xml.svg\n.. image:: https://static.pepy.tech/personalized-badge/json2xml?period=total\u0026units=international_system\u0026left_color=blue\u0026right_color=orange\u0026left_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**Looking for a Go version?** Check out `json2xml-go \u003chttps://github.com/vinitkumar/json2xml-go\u003e`_, a Go port of this library with identical features and a native CLI tool.\n\n\n\nArchitecture Diagram\n^^^^^^^^^^^^^^^^^^^^\n\n.. image:: ./diagram.png\n\n\nInstallation\n^^^^^^^^^^^^\n\n**As a Library**\n\n.. code-block:: console\n\n    pip install json2xml\n\n**With Native Rust Acceleration (28x faster)**\n\nFor maximum performance, install the optional Rust extension:\n\n.. code-block:: console\n\n    # Install json2xml with Rust acceleration\n    pip install json2xml[fast]\n    \n    # Or install the Rust extension separately\n    pip install json2xml-rs\n\nThe Rust extension provides **28x faster** conversion compared to pure Python. It's automatically used when available, with seamless fallback to pure Python.\n\n**As a CLI Tool**\n\nThe library includes a command-line tool ``json2xml-py`` that gets installed automatically:\n\n.. code-block:: console\n\n    pip install json2xml\n\n    # Now you can use it from the command line\n    json2xml-py data.json\n    json2xml-py -s '{\"name\": \"John\", \"age\": 30}'\n    json2xml-py -u https://api.example.com/data.json\n\nFor CLI options, run ``json2xml-py --help``.\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* Compliant with the `json-to-xml` function specification from `XPath 3.1 \u003chttps://www.w3.org/TR/xpath-functions-31/#func-json-to-xml\u003e`_\n* **Command-line tool** for easy conversion from the terminal\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    \u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n    \u003call\u003e\n      \u003clogin type=\"str\"\u003emojombo\u003c/login\u003e\n      \u003cid type=\"int\"\u003e1\u003c/id\u003e\n      \u003cavatar_url type=\"str\"\u003ehttps://avatars0.githubusercontent.com/u/1?v=4\u003c/avatar_url\u003e\n    \u003c/all\u003e\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 \u003citem\u003e\u003c/item\u003e.\n\nDefault output:\n\n.. code-block:: xml\n\n    \u003c?xml version=\"1.0\" ?\u003e\n    \u003call\u003e\n      \u003cmy_items type=\"list\"\u003e\n        \u003citem type=\"dict\"\u003e\n          \u003cmy_item type=\"dict\"\u003e\n            \u003cid type=\"int\"\u003e1\u003c/id\u003e\n          \u003c/my_item\u003e\n        \u003c/item\u003e\n        \u003citem type=\"dict\"\u003e\n          \u003cmy_item type=\"dict\"\u003e\n            \u003cid type=\"int\"\u003e2\u003c/id\u003e\n          \u003c/my_item\u003e\n        \u003c/item\u003e\n      \u003c/my_items\u003e\n      \u003cmy_str_items type=\"list\"\u003e\n        \u003citem type=\"str\"\u003ea\u003c/item\u003e\n        \u003citem type=\"str\"\u003eb\u003c/item\u003e\n      \u003c/my_str_items\u003e\n      \u003cempty type=\"list\"/\u003e\n    \u003c/all\u003e\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    \u003c?xml version=\"1.0\" ?\u003e\n    \u003call\u003e\n      \u003cmy_items type=\"list\"\u003e\n        \u003cmy_item type=\"dict\"\u003e\n          \u003cid type=\"int\"\u003e1\u003c/id\u003e\n        \u003c/my_item\u003e\n        \u003cmy_item type=\"dict\"\u003e\n          \u003cid type=\"int\"\u003e2\u003c/id\u003e\n        \u003c/my_item\u003e\n      \u003c/my_items\u003e\n      \u003cmy_str_items type=\"str\"\u003ea\u003c/my_str_items\u003e\n      \u003cmy_str_items type=\"str\"\u003eb\u003c/my_str_items\u003e\n    \u003c/all\u003e\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     \u003c?xml version=\"1.0\" ?\u003e\n     \u003call\u003e\n       \u003clogin\u003emojombo\u003c/login\u003e\n       \u003cid\u003e1\u003c/id\u003e\n       \u003cavatar_url\u003ehttps://avatars0.githubusercontent.com/u/1?v=4\u003c/avatar_url\u003e\n     \u003c/all\u003e\n\n\nXPath 3.1 Compliance Options\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nThe library supports the optional `xpath_format` parameter which makes the output compliant with the `json-to-xml` function specification from `XPath 3.1 \u003chttps://www.w3.org/TR/xpath-functions-31/#func-json-to-xml\u003e`_. When enabled, the XML output follows the standardized format defined by the W3C specification.\n\n.. code-block:: python\n\n     from json2xml import json2xml\n     from json2xml.utils import readfromstring\n\n     data = readfromstring(\n         '{\"login\":\"mojombo\",\"id\":1,\"avatar_url\":\"https://avatars0.githubusercontent.com/u/1?v=4\"}'\n     )\n     # Use xpath_format=True for XPath 3.1 compliant output\n     print(json2xml.Json2xml(data, xpath_format=True).to_xml())\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**Rust Extension Development**\n\nThe optional Rust extension (``json2xml-rs``) provides 29x faster performance. To develop or build the Rust extension:\n\nPrerequisites:\n\n .. code-block:: console\n\n    # Install Rust (if not already installed)\n    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh\n    \n    # Install maturin (Rust-Python build tool)\n    uv pip install maturin\n\nBuilding the extension:\n\n .. code-block:: console\n\n    # Development build (installs in current environment)\n    cd rust\n    uv pip install -e .\n    \n    # Or using maturin directly\n    maturin develop --release\n    \n    # Production wheel build\n    maturin build --release\n\nRunning Rust benchmarks:\n\n .. code-block:: console\n\n    # After building the extension\n    python benchmark_rust.py\n\nThe Rust code is in ``rust/src/lib.rs`` and uses PyO3 for Python bindings.\n\n\nCLI Usage\n^^^^^^^^^\n\nThe ``json2xml-py`` command-line tool provides an easy way to convert JSON to XML from the terminal.\n\n**Basic Examples**\n\n.. code-block:: console\n\n    # Convert a JSON file to XML\n    json2xml-py data.json\n\n    # Convert with custom wrapper element\n    json2xml-py -w root data.json\n\n    # Read JSON from string\n    json2xml-py -s '{\"name\": \"John\", \"age\": 30}'\n\n    # Read from stdin\n    cat data.json | json2xml-py -\n\n    # Output to file\n    json2xml-py -o output.xml data.json\n\n    # Use XPath 3.1 format\n    json2xml-py -x data.json\n\n    # Disable pretty printing and type attributes\n    json2xml-py --no-pretty --no-type data.json\n\n**CLI Options**\n\n.. code-block:: text\n\n    Input Options:\n      -u, --url string        Read JSON from URL\n      -s, --string string     Read JSON from string\n      [input-file]            Read JSON from file (use - for stdin)\n\n    Output Options:\n      -o, --output string     Output file (default: stdout)\n\n    Conversion Options:\n      -w, --wrapper string    Wrapper element name (default \"all\")\n      -r, --root              Include root element (default true)\n      -p, --pretty            Pretty print output (default true)\n      -t, --type              Include type attributes (default true)\n      -i, --item-wrap         Wrap list items in \u003citem\u003e elements (default true)\n      -x, --xpath             Use XPath 3.1 json-to-xml format\n      -c, --cdata             Wrap string values in CDATA sections\n      -l, --list-headers      Repeat headers for each list item\n\n    Other Options:\n      -v, --version           Show version information\n      -h, --help              Show help message\n\n\nGo Version\n^^^^^^^^^^\n\nA Go port of this library is available at `json2xml-go \u003chttps://github.com/vinitkumar/json2xml-go\u003e`_.\n\n**Install the Go CLI:**\n\n.. code-block:: console\n\n    go install github.com/vinitkumar/json2xml-go/cmd/json2xml@latest\n\nThe Go version provides the same features and a native compiled binary for maximum performance.\n\n\nRust Extension (PyO3)\n^^^^^^^^^^^^^^^^^^^^^\n\nFor users who need maximum performance within Python, json2xml includes an optional native Rust extension built with PyO3:\n\n.. code-block:: console\n\n    pip install json2xml[fast]\n\n**Rust vs Pure Python Performance:**\n\n.. list-table::\n   :header-rows: 1\n   :widths: 30 20 20 15\n\n   * - Test Case\n     - Pure Python\n     - Rust Extension\n     - Speedup\n   * - **Small JSON** (47 bytes)\n     - 40µs\n     - 1.5µs\n     - **27x**\n   * - **Medium JSON** (3.2 KB)\n     - 2.1ms\n     - 71µs\n     - **30x**\n   * - **Large JSON** (32 KB)\n     - 21ms\n     - 740µs\n     - **28x**\n   * - **Very Large JSON** (323 KB)\n     - 213ms\n     - 7.5ms\n     - **28x**\n\n**Usage with Rust Extension:**\n\n.. code-block:: python\n\n    # Automatic backend selection (recommended)\n    from json2xml.dicttoxml_fast import dicttoxml, get_backend\n\n    print(f\"Using backend: {get_backend()}\")  # 'rust' or 'python'\n    \n    data = {\"name\": \"John\", \"age\": 30}\n    xml_bytes = dicttoxml(data)\n\nThe ``dicttoxml_fast`` module automatically uses the Rust backend when available and falls back to pure Python for unsupported features (like ``xpath_format``, ``xml_namespaces``, or custom ``item_func``).\n\n**Platform Support:**\n\nPre-built wheels are available for:\n\n- Linux (x86_64, aarch64)\n- macOS (x86_64, arm64/Apple Silicon)\n- Windows (x86_64)\n\nFor other platforms, the pure Python version is used automatically.\n\n\nPerformance Benchmarks\n^^^^^^^^^^^^^^^^^^^^^^\n\nComprehensive benchmarks comparing all implementations (Apple Silicon, January 2026):\n\n.. list-table::\n   :header-rows: 1\n   :widths: 22 18 15 15 15 15\n\n   * - Test Case\n     - Python\n     - Rust\n     - Go\n     - Zig\n     - Best\n   * - **Small** (47B)\n     - 40µs\n     - 1.5µs\n     - 4.6ms\n     - 3.7ms\n     - Rust (28x)\n   * - **Medium** (3.2KB)\n     - 2.1ms\n     - 71µs\n     - 4.1ms\n     - 3.3ms\n     - Rust (30x)\n   * - **Large** (32KB)\n     - 21ms\n     - 740µs\n     - 4ms\n     - 6.1ms\n     - Rust (28x)\n   * - **Very Large** (323KB)\n     - 213ms\n     - 7.5ms\n     - 4.4ms\n     - 33ms\n     - Go (48x)\n\n**Key Findings:**\n\n- **Rust extension**: ~28x faster than Python, zero overhead (best for Python users)\n- **Go CLI**: 48x faster for large files (300KB+), but has ~4ms startup overhead\n- **Zig CLI**: 3-6x faster for medium-large files\n\n**Recommendation by Use Case:**\n\n- **Python library calls**: Use ``pip install json2xml[fast]`` (Rust, 28x faster)\n- **Large file CLI processing**: Use `json2xml-go \u003chttps://github.com/vinitkumar/json2xml-go\u003e`_ (Go, 48x for 300KB+)\n- **Pure Python required**: Use ``pip install json2xml``\n\nFor detailed benchmarks, see `BENCHMARKS.md \u003chttps://github.com/vinitkumar/json2xml/blob/master/BENCHMARKS.md\u003e`_.\n\n\nOther Implementations\n^^^^^^^^^^^^^^^^^^^^^\n\nThis library is also available in other languages:\n\n- **Rust**: `json2xml-rs \u003chttps://pypi.org/project/json2xml-rs/\u003e`_ - 28x faster, Python extension via PyO3\n- **Go**: `json2xml-go \u003chttps://github.com/vinitkumar/json2xml-go\u003e`_ - 48x faster for large files, native CLI\n- **Zig**: `json2xml-zig \u003chttps://github.com/nicholasgriffintn/json2xml-zig\u003e`_ - 6x faster, native CLI\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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvinitkumar%2Fjson2xml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvinitkumar%2Fjson2xml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvinitkumar%2Fjson2xml/lists"}