XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

Converting Between XML and JSON
by Stefan Goessner | Pages: 1, 2, 3

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:

<ol class="xoxo">
  <li>Subject 1
    <ol>
        <li>subpoint a</li>
        <li>subpoint b</li>
    </ol>
  </li>
  <li><span>Subject 2</span>
    <ol compact="compact">
        <li>subpoint c</li>
        <li>subpoint d</li>
    </ol>
  </li>
</ol>

Now we apply the patterns above to convert this XML document fragment to a JSON structure.

  1. The outer list with two list items is converted using pattern 6.
  2. 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.
  3. The first inner list is converted with pattern 6 again.
  4. Pattern 5 is applied to the second item of the outer list.
  5. 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.

"ol": {
  "li": [ 
    {
      "#text": "Subject 1",
      "ol": {
        "li": ["subpoint a", "subpoint b"]
      }
    },
    {
      "span": "Subject 2",
      "ol": {
        "@compact": "compact",
        "li": ["subpoint c", "subpoint d"]
      }
    }
  ]
}

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.

<span class="vevent">
  <a class="url" href="http://www.web2con.com/">
    <span class="summary">Web 2.0 Conference</span>
    <abbr class="dtstart" title="2005-10-05">October 5</abbr>
    <abbr class="dtend" title="2005-10-08">7</abbr>
    <span class="location">Argent Hotel, San Francisco, CA</span>
  </a>
</span>

Here, patterns 2, 3, 4, 5 and 6 are used to generate the following JSON structure:

"span": {
  "a": {
    "@class": "url",
    "@href": "http://www.web2con.com/",
    "span": [
      { "@class": "summery", "#text": "Web 2.0 Conference" },
      { "@class": "location", "#text": "Argent Hotel, San Francisco, CA" }
    },
    "abbr": [
      { "@class": "dtstart", "title": "2005-10-05", "#text": "October 5" },
      { "@class": "dtend", "title": "2005-10-08", "#text": "7" }
    }
  }
}

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:

  1. state that a conversion isn't sufficiently possible.
  2. tolerate the result if order doesn't matter.
  3. 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

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.



1 to 10 of 10
  1. e4x changes this
    2010-04-27 10:48:19 netdragon
  2. Improved version
    2008-01-28 07:30:50 MichaelSchoeler
  3. Look at XSugar for bi-directional translation
    2007-06-27 02:03:10 robertmue
  4. Congratulations! Could you help me with Json-lib?
    2007-05-03 13:34:05 CassiuP
  5. I have a problem about json
    2007-04-27 02:43:29 peter.wang
  6. tab is not really optional in xml2json
    2006-11-16 15:39:41 tschaub
  7. What happens to empty elements?
    2006-10-09 12:24:03 babbage
  8. Interesting!
    2006-06-18 10:17:47 ThomasFrank
  9. Javascript?
    2006-06-13 04:33:29 chrisbp
  10. #text should always work
    2006-05-31 15:49:30 bkc
1 to 10 of 10