Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Python script converts XML to JSON or the other way around
Pull request Compare This branch is 43 commits behind hay:master.
Latest commit 21754b3 @hay hay Added pre tags
Failed to load latest commit information. Added pre tags
xml2json Added files


Python script converts XML to JSON or the other way around


Make this executable

$ chmod u+x xml2json

Then invoke it from the command line like this

$ xml2json -t xml2json -f file.xml file.json

Or the other way around

$ xml2json -t json2xml -f file.json file.xml

You can also output to stdout

$ xml2json -t json2xml -f file.json -o

More info

Relies on ElementTree for the XML parsing. This is based on but uses a different XML->JSON mapping. The XML->JSON mapping is described at

Rewritten to a command line utility by Hay Kranen: Fork it on Github:

XML                              JSON
                             "e": null
text                      "e": "text"
               "e": { "@name": "value" }
text         "e": { "@name": "value", "#text": "text" }
 texttext  "e": { "a": "text", "b": "text" }
 text text  "e": { "a": ["text", "text"] }
 text text         "e": { "#text": "text", "a": "text" }

This is very similar to the mapping used for Yahoo Web Services:

This is a mess in that it is so unpredictable -- it requires lots of testing (e.g. to see if values are lists or strings or dictionaries). For use in Python this could be vastly cleaner. Think about whether the internal form can be more self-consistent while maintaining good external characteristics for the JSON.

Look at the Yahoo version closely to see how it works. Maybe can adopt that completely if it makes more sense...

R. White, 2006 November 6

Something went wrong with that request. Please try again.