Package: 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 thedict2xmlproject, 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.Featuresjson2xml supports the following features:Conversion from ajsonstring to XMLConversion from ajsonfile to XMLConversion from an API that emitsjsondata to XMLUsageYou can use the json2xml library in the following ways:fromjson2xmlimportjson2xmlfromjson2xml.utilsimportreadfromurl,readfromstring,readfromjson# Convert JSON data from a URL to XMLdata=readfromurl("https://api.publicapis.org/entries")print(json2xml.Json2xml(data).to_xml())# Convert a JSON string to XMLdata=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 XMLdata=readfromjson("examples/licht.json")print(json2xml.Json2xml(data).to_xml())Custom Wrappers and IndentationBy default, a wrapperalland prettyTrueis set. However, you can easily change this in your code like this:fromjson2xmlimportjson2xmlfromjson2xml.utilsimportreadfromurl,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:<?xml version="1.0" ?><all><logintype="str">mojombo</login><idtype="int">1</id><avatar_urltype="str">https://avatars0.githubusercontent.com/u/1?v=4</avatar_url></all>Omit List itemAssume 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_itemstype="list"><itemtype="dict"><my_itemtype="dict"><idtype="int">1</id></my_item></item><itemtype="dict"><my_itemtype="dict"><idtype="int">2</id></my_item></item></my_items><my_str_itemstype="list"><itemtype="str">a</item><itemtype="str">b</item></my_str_items><emptytype="list"/></all>However, you can change this behavior using the item_wrap property like this:fromjson2xmlimportjson2xmlfromjson2xml.utilsimportreadfromurl,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:<?xml version="1.0" ?><all><my_itemstype="list"><my_itemtype="dict"><idtype="int">1</id></my_item><my_itemtype="dict"><idtype="int">2</id></my_item></my_items><my_str_itemstype="str">a</my_str_items><my_str_itemstype="str">b</my_str_items></all>Optional Attribute Type SupportYou can also specify if the output XML needs to have type specified or not. Here is the usage:fromjson2xmlimportjson2xmlfromjson2xml.utilsimportreadfromurl,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:<?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 testsThis 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 -vvHelp and Support to maintain this projectYou can sponsor my work for this plugin here:https://github.com/sponsors/vinitkumar/History4.1.0 / 2023-11-07chore(deps): bump urllib3 from 1.26.13 to 1.26.18 (#192, #195)4.0.1 / 2023-10-13feat: get local working for 3.12 and fix some code quality issueFix/make docs better (#186)fix: issue with hardcoded yearfeat: upgrade sphinx themefix: issues with ruff linting, use isinstance in place of typefix: make readme betterchore(deps): bump tornado from 6.3.2 to 6.3.3 in /docs (#185)chore(deps): bump certifi from 2022.12.7 to 2023.7.22 in /docs (#183)chore(deps): bump pygments from 2.10.0 to 2.15.0 in /docs (#182)4.0.0 / 2023-07-19Updated to Python 3.12 beta 4Added check for pypy 3.10Dropped support for Python 3.7Updated fixture for attrs with dict and removed comment (#170)3.21.0 / 2023-01-18Implemented list item with attributes (#169)Added python3.12 alpha4 to the fix (#168)Replaced requests with urllib3 (#167)Added security reporting guidelinesAdded CodeQL workflow for GitHub code scanning (#160)Set default to False for list_headers (#164)Fixed ci issue due to mypy update on python3.11 (#166)Updated certifi from 2021.10.8 to 2022.12.7 in /docs (#162)Fixed file opening issueCelebrated release of Python 3.11 (#159)3.20.0 / 2022-10-16Made dependencies more flexible (#158)Used SystemRandom for secure random integer generation (#156)3.19.5 / 2022-09-18Fixed issues #138 and #151, added 2 new unit tests (#154)Fixed unit tests for #152 (#153)3.19.4 / 2022-07-24Transitioned from unittest to pytest (#149)Upgraded python test to python311beta (#148)Tested new version of 3.10 and 3.11 (#147)3.19.3 / 2022-07-01Added UTF-8 encoding type to @readfromjson function in utils.py for Korean language support (#145)3.19.2 / 2022-06-09Escaped xml char when having attrs (#144)Adjusted pytest config setting for easier loggingBumped python 3.10 and 3.11 version (#142)3.19.1 / 2022-06-05Bumped version of docs buildingUpdated waitress from 2.1.1 to 2.1.2 in /docs (#141)Updated docs for dicttoxml (#140)3.19.0 / 2022-05-20Set xsi location (#135)Repeated list headers (#138)Added support for python3.11 (#139)Improved docs for dicttoxml (#134)Fixed types working check for ci mypy (#133)Added mypy support to ci (#132)Generated changelogRemoved logging by default (#131)Merged two dev requirements files (#129)Removed old unused configAdded types (#125)Fixed issue with twine checkFixed issue with long descriptionRefactored: xmltodict is only test dependency now (#124)Added correct list of contributorsGenerated changelogImproved dicttoxml (#121)Added correct badgeStarted using codecovFixed flake8 testsAdded coverage to the mixFixed lint issues and CIChecked new CI stuff like lint and coverageBumped version and generated changelogFixed issue with wrong output for boolean listMade pull requests trigger action runs3.18.0 / 2022-04-23Bumped versionImproved dicttoxml (#121)Added correct badgeStarted using codecovFixed flake8 testsAdded coverage to the mixFixed lint issues and CIChecked new CI stuff like lint and coverageBumped version and generated changelogFixed issue with wrong output for boolean listMade pull requests trigger action runs3.17.1 / 2022-04-20Fixed issue with wrong output for boolean listMade pull requests trigger action runs3.17.0 / 2022-04-18Fixed return of correct xml type for bool (#119)Added download counterChecked latest alpha (#116)Checked latest alpha (#115)Updated waitress from 2.0.0 to 2.1.1 in /docs (#114)Only python3 wheels are created now3.15.0 / 2022-02-24Merged remote-tracking branch ‘origin/master’Bumped version and prepared for new releaseAdded new python versions to test against (#110)Fixed perflint (#109)Supported latest version of 3.10 and 3.11 alpha3 (#98)Generated changelogRemoved unused importsBumped versionFixed issue with uncaught UnicodeDecodeErrorCancelled jobs for concurrent builds in same PRStabilized pypiUpdated tox configv3.14.0 / 2022-02-10Removed unused importsBumped versionFixed issue with uncaught UnicodeDecodeErrorfix: remove unused importsbump versionfix: issue with uncaught UnicodeDecodeErrorcancel jobs for concurrent builds in same PRpypi is stable nowfeat: update tox configv3.11.0 / 2022-01-31bump versionfeat: remove commentsFeat: install pytest separately and run pytests nowfix toxadd some documentation on testingsplit testing libs away from releasefix: update changelogbump version to 3.10.0fix: we support Python3.7+ now (#101)Issue: #99 dicttoxml igores the root param (#100)v3.10.0 / 2022-01-29bump version to 3.10.0fix: we support Python3.7+ now (#101)Issue: #99 dicttoxml igores the root param (#100)feat: bump to a rc1 versionAdd support for Python3.11 alpha and upgrade pytest and py (#97)Feat: drop 3.11.0 alphas from the test matrix for nowfeat: find the versions that are in the CIfix: typo in the name of python 3.11 versionsunsetting python 3.6 and add support for python3.11 alphachore: prepare for release 3.9.0fix emailfix readmeupdate readme - add tests - refactorresolve #93chore: run black on readme docfix: more issuesfix: garbage in historyfeat: generate historyv3.9.0 / 2021-12-19feat: generate historyfeat: item_wrap for str and int (#93)v3.8.4 / 2021-10-24bump versionfix: version bump and readme generatorv3.8.3 / 2021-10-24bump versionfeat: 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-07Feat/security improvements (#81)arrow_up:feat: python 3.10 released (#79)v3.7.0 / 2021-09-11bookmark:feat: final release for v3.7.0bookmark:feat: bump versionv3.7.0beta2 / 2021-09-10Feat/cleanup and deprecation fix (#78)item ommision (#76)Create FUNDING.ymlv3.7.0beta1 / 2021-08-28Feat/fork and update dict2xml (#75)chore(deps-dev): bump pip from 18.1 to 19.2 (#73)Delete .travis.ymlchore(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-12Feat/wip exceptions (#65)Add .deepsource.tomlfeat: upgrade the actionsfeat: try & support more os and python versionsUpdate pythonpackage.ymlv3.5.0 / 2020-08-24feat: remove six as dependency as we are python3 only, resolves #60 (#61)feat: update makefile for the correct commandv3.4.1 / 2020-06-10fix: issues with pypi release and bump versionFeat/attr type docs (#58)fix: conflictsFeat/attr type docs (#57)Merge github.com:vinitkumar/json2xmlUpdate json2xml.py (#56)Merge github.com:vinitkumar/json2xmlfeat: fix typo in the readmev3.3.3 / 2020-02-05Update README.rstfix: issue with pypi uploadsfix: versionbump versionUpdate pythonpackage.ymlRefactor/prospector cleanup (#50)Update pythonpackage.ymlCreate pythonpackage.ymlUpdate README.rstfix: typo in readmebump versionFeature/attribute support (#48)Feature/attribute support (#47)chore: bump versionfix: remove print statement in json read because it confuses peoplefix typo in readmev3.0.0 / 2019-02-26Fix/coveralls (#43)update coverage report (#42)Merge pull request #41 from vinitkumar/fix/coverallsadd python coverallsMerge pull request #40 from vinitkumar/refactor/cookiecutterupdate coverageadd image for coverallscoverage and coveralls integrationstry and trigger coveralls toofix code block in readmeadd doc about custom wrappertry at reducing the dependenciesadd tests for custom wrappers as welladd tests for actualy dict2xml conversionfix: remove missing importfix: code syntax highlight in the readme againfix: code syntax highlight in the readme againfix: code syntax highlight in the readmechore: update readme with code samplestest: add testcases for the different utils methodremove unused importscheck the third method for generating dict from json string toorun correct test filesfix testsupdate requirements and setuptoolsrefactor the module into more maintainable codechore: add boilerplateremove all legacyFix/cleanup (#38)cleanup: remove unused modules (#37)Merge pull request #35 from vinitkumar/improve-structurecleanupone again try to get the build workingtravis need full version for latest supported pythondo not hardcode version in a seriesupdate grammarfix conflictsUpdate LICENSEcleanup readmeremove clisome cleanup and update the testsUpdate readme.mdCleanup Readme.mdUpdate issue templatesfix vulnerabilities in requests