Python script converts XML to JSON or the other way around
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is 47 commits behind hay:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information. Added pre tags Sep 28, 2010
xml2json Added files Sep 28, 2010


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