Converting Between XML and JSON
May 31, 2006
XmlToJSON is a JavaScript function which converts XML to JSON. Convert XML to JSON with JavaScript to work with the JSON format instead of the XML format.
Download convert xml to json with python. 1/1/2018 0 Comments JSON with Python. Python script converts XML to JSON or the other way around. Make this executable. Then invoke it from the command line like this. Or the other way around. Without the -o parameter xml2json writes to stdout. Df.tojson(r'Path to store the exported JSON file File Name.json') For example, the path where I’ll be storing the exported JSON file is: C: Users Ron Desktop ExportDataFrame.json. You’ll need to adjust the path (in the Python code below) to reflect the location where you’d like to store the JSON file on your computer.
More and more web service providers seem to be interested inofferingJSONAPIs beneath their XML APIs. One considerable advantage of using a JSON API is its ability to provide cross-domain requests while bypassing the restrictive same domain policy of the XmlHttpRequest object. On the client-side, JSON comes with a native language-compliant data structure, with which it performs much better than corresponding DOM calls required for XML processing. Finally, transforming JSON structures to presentational data can be easily achieved with tools such as JSONT.
So if you're working in this space, you probably need to convert an existing XML document to a JSON structure while preserving the following:
- structure
- order
- information
In an ideal world, the resulting JSON structure can be converted back to its original XML document easily. Thus it seems worthwhile to discuss some common patterns as the foundation of a potentially bidirectional conversion process between XML and JSON. A similar discussion can be found at BadgerFish and Yahoo-- without the reversibility aspect though.
A Pragmatic Approach
A single structured XML element might come in seven flavors:
- an empty element
- an element with pure text content
- an empty element with attributes
- an element with pure text content and attributes
- an element containing elements with different names
- an element containing elements with identical names
- an element containing elements and contiguous text
The following table shows the corresponding conversion patterns between XML and JSON.
Pattern | XML | JSON | Access |
1 | <e/> | 'e': null | o.e |
2 | <e>text</e> | 'e': 'text' | o.e |
3 | <e name='value' /> | 'e':{'@name': 'value'} | o.e['@name'] |
4 | <e name='value'>text</e> | 'e': { '@name': 'value', '#text': 'text' } | o.e['@name'] o.e['#text'] |
5 | <e> <a>text</a> <b>text</b> </e> | 'e': { 'a': 'text', 'b': 'text' } | o.e.a o.e.b |
6 | <e> <a>text</a> <a>text</a> </e> | 'e': { 'a': ['text', 'text'] } | o.e.a[0] o.e.a[1] |
7 | <e> text <a>text</a> </e> | 'e': { '#text': 'text', 'a': 'text' } | o.e['#text'] o.e.a |
Please note that all patterns are considered to describe structured elements, despite the fact that the element of pattern 7 is commonly understood as a semistructured element. A pragmatic approach to convert an XML document to a JSON structure and vice versa can be based on the seven patterns above. It always assumes a normalized XML document for input and doesn't take into consideration the following:
- XML declaration
- processing instructions
- explicit handling of namespace declarations
- XML comments
Preserving order
JSON is built on two internal structures:
- A collection of name/value pairs with unique names (associative array)
- An ordered list of values (array)
An attempt to map a structured XML element...
...to the following JSON object:
yields an invalid result, since the name 'a'
is not unique in the associative array. So we need to collect all elements of identical names in an array. Using the patterns 5 and 6 above yields the following result:
Now we have a structure that doesn't preserve element order. This may or may not be acceptable, depending on whether the above XML element order matters.
So, our general rules of thumb are:
A structured XML element can be converted to a reversible JSON structure, if
- all subelement names occur exactly once, or …
- subelements with identical names are in sequence.
Xml To Json Online Converter
and
A structured XML element can be converted to an irreversible but semantically equivalent JSON structure, if
- multiple homonymous subelements occur nonsequentially, and …
- element order doesn't matter.
If none of these two conditions apply, there is no pragmatic way to convert XML to JSON using the patterns above. Here, SVG and SMIL documents, which implicitly rely on element order, come to mind.
Semi-Structured XML
XML documents can contain semi-structured elements, which are elements with mixed content of text and child elements, usually seen in documentation markup. If the textual content is contiguous, as in:
we can apply pattern 7 and yield the following for this special case:
But how do we convert textual content mixed up with elements? For example:
It obviously doesn't make sense in most cases to collect all text nodes in an array,
that doesn't preserve order or semantics.
So the best pragmatic solution is to treat mixed semi-structured content in JSON the same way as XML treats CDATA sections -- as unknown markup.
Another rule is that XML elements with
- mixed content of text and element nodes and
- CDATA sections
are converted to a reversible JSON string containing the complete XML markup according to pattern 2 or 4.
Examples
Now let's look at two examples using the insight we've gained thus far. Microformats are well suited because they are an open standard and short enough for a brief discussion.
XOXO, as a simple XHTML-based outline format, is one of several microformats. The slightly modified sample from the Draft Specification reads:
Now we apply the patterns above to convert this XML document fragment to a JSON structure.
- The outer list with two list items is converted using pattern 6.
- The first list item contains a single textual content 'Subject 1' and an inner list element. So, it can be treated according to pattern 7.
- The first inner list is converted with pattern 6 again.
- Pattern 5 is applied to the second item of the outer list.
- The second inner list is converted using a combination of patterns 3 and 6.
Here is the resulting JSON structure, which is reversible without losing any information.
hCalendar is another microformat based on the iCalendar standard. We'll just ignore the fact that the iCalendar format could be more easily converted to JSON, and will look at an hCalendar event example, which is also slightly modified so that it is a structured, rather than mixed, semi-structured document fragment.
Here, patterns 2, 3, 4, 5 and 6 are used to generate the following JSON structure:
This example demonstrates a conversion that does not preserve the original element order. Even if this may not change semantics here, we can do the following:
- state that a conversion isn't sufficiently possible.
- tolerate the result if order doesn't matter.
- try to make our XML document more JSON-friendly.
In many cases the last point may be not acceptable, at least when the XML document is based on existing standards. But in other cases, it may be worth the effort to consider some subtle XML changes, which can make XML and JSON play nicely together. Changing the <abbr>
elements to <span>
elements in the hCalendar example would be an improvement.
XML is a document-centric format, while JSON is a format for structured data. This fundamental difference may be irrelevant, as XML is also capable of describing structured data. If XML is used to describe highly structured documents, these may play very well together with JSON.
Problems may arise, if XML documents do the following:
- implicitly rely on element order
- contain a lot of semi-structured data
As proof of this concept, I have implemented two Javascript functions -- xml2json
and json2xml
-- based on the six patterns above, which can be used for the following:
- client-side conversion
- a parsed XML document via DOM to a JSON structure
- a JSON structure to a (textual) XML document
- implementing converters in other server side languages
Download Convert Xml To Json With Python Syntax
Future XML document design may be influenced by these or similar patterns in order to get the best of both the XML and JSON worlds.