xml to json mapping challenge xml to json mapping challenge json json

xml to json mapping challenge


There are established mappings from XML to JSON with limitations (see Converting Between XML and JSON) and mappings from JSON to XML (see JSONx as defined here and conversion rules by IBM). A mapping from XML to JSON that preserves order, however, has not been defined yet. To fully capture all aspects of XML, one should express the XML Infoset in JSON. if you only care about XML elements (no processing instructions, etc.), I'd choose this structure:

[  "parentNode",  { } /* attributes */  [     [ "fooNode", { }, [ "data1" ] ]    [ "fooNode", { }, [ "data2" ] ]    [ "fooNode", { }, [ "data3" ] ]  ]]

I implemented the same mapping as mapping between XML and Perl data structures that are just like JSON with XML::Struct. The structure further corresponds to the abstract data model of MicroXML, a simplified subset of XML.


If you need the same element name often and you care about ordering it might be better to stay with XML. What benefits do you expect from using JSON?


Why not try:

{ parentNode: [  ["fooNode", "data1"],  ["barNode", "data2"],  ["fooNode", "data3"] ]}

I think it would more or less solve the problem.

And yes, I think you should abandon automatic conversion if it's not flexible enough; instead you might look for an API that makes such mappings trivial.