http://www.zorba-xquery.com/modules/json

Module Description

In the XQuery specification, XQuery is described as a language capable of expressing queries on XML data. This makes XQuery the perfect choice for data processing on the Web because XML is widely used there. However, there are also some other data formats present on the Web. For example, JSON (JavaScript Object Notation) is the most common data format for applications which are written in JavaScript. JSON is a lightweight hierarchical data-interchange format. Like XML, it is easy for humans to read and write. Moreover, it is easy for machines to parse and generate.

In order to enable JSON processing with XQuery, Zorba implements a set of functions that open XQuery developers the door to process JSON data. Specifically, this module provides two types of functions. Functions to

Both types of functions are available to parse and serialize two types of XDM-JSON mappings, respectively. The first mapping has been proposed by John Snelson. The second mapping is called JsonML. In the following, we briefly describe both mappings.

Simple XDM-JSON Mapping

In order to process JSON with XQuery, Zorba implements a mapping between JSON and XML that was initially proposed by John Snelson in his article Parsing JSON into XQuery. In this article, he describes the following recursive mapping declarations.

JSON type(JSON) toXML(JSON)
JSON N/A <json type="type(JSON)">toXML(JSON)</json>
{ "key1": value1, "key2": value2 } object <pair name="key1" type="type(value1)">toXML(value1)</pair><pair name="key2" type="type(value2)">toXML(value2)</pair>
[ value1, value2 ] array <item type="type(value1)">toXML(value1)</item><item type="type(value2)">toXML(value2)</item>
"value" string value
number number number
true/false boolean true/false
null null empty

Zorba implements the simple mapping in two functions:

JsonML Mapping

JSonML (JSON Markup Language) is an application of the JSON format. The purpose of JSonML is to provide a compact format for transporting XML-based markup as JSon. In contrast to the simple JSON mapping described above JsonML allows a lossless conversion back and forth.

Zorba implements the JsonML-XDM mapping in two functions:

Author:

Sorin Nasoi

Function Summary
parse($text as xs:string*) as node()*
parse-ml($text as xs:string*) as node()*
serialize($xml as node()*) as xs:string*
serialize-ml($xml as node()*) as xs:string*
Functions
parse
declare function json:parse (
$text as xs:string*
) as node()* external

This function parses a JSON string and returns an XDM instance according to the simple JSON-XDM mapping described above.

Query:

              
 json:parse('{
      "firstName": "John",
      "lastName": "Smith",
      "address": {
          "streetAddress": "21 2nd Street",
          "city": "New York",
      },
      "phoneNumbers": [
          "646 123-4567"
      ]
  }')
            

Result:

              
 <json type="object">
   <pair name="firstName" type="string">John</pair>
   <pair name="lastName" type="string">Smith</pair>
   <pair name="address" type="object">
     <pair name="streetAddress" type="string">21 2nd Street</pair>
     <pair name="city" type="string">New York</pair>
   </pair>
   <pair name="phoneNumbers" type="array">
     <item type="string">212 732-1234</item>
   </pair>
 </json>
            

Parameters:
$text - a sequence of valid JSON strings
Returns:

a sequence of nodes according to the simple JSON-XDM mapping

Errors
API0060 if any of the strings passed as parameter is not valid JSON.

serialize
declare function json:serialize (
$xml as node()*
) as xs:string* external

The serialize function takes a sequence of nodes as parameter and transforms each element into a valid JSON string according to the simple mapping described above.

Query:

              
 json:serialize(
   <json type="object">
     <pair name="firstName" type="string">John</pair>
     <pair name="lastName" type="string">Smith</pair>
     <pair name="address" type="object">
       <pair name="streetAddress" type="string">21 2nd Street</pair>
       <pair name="city" type="string">New York</pair>
     </pair>
     <pair name="phoneNumbers" type="array">
       <item type="string">212 732-1234</item>
     </pair>
   </json>
 )
            

Result:

              
 {
   "firstName": "John",
   "lastName": "Smith",
   "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
   },
   "phoneNumbers": [
     "212 732-1234"
   ]
 }
            

Parameters:
$xml - a sequence of nodes conformant to the simple mapping described above
Returns:

a sequence of JSON strings

Errors
API0061 if the passed elements do not have a valid JSON structure

parse-ml
declare function json:parse-ml (
$text as xs:string*
) as node()* external

This function parses JSON and returns an XDM instances according to the JsonML mapping described above.

Query:

              
 json:parse-ml(
   '["table", {"class":"maintable"},
      ["tr", {"class":"odd"},
        ["th", {}, "Situation"],
        ["th", "Result"] ],
      ["tr", {"class":"even"},
        ["td", ["a",  {"href" : "driving.html", "title" : "Driving"}, "Driving"]],
        ["td", "Busy"] ]
    ]'
 )
            

Result:

              
 <table class="maintable">
   <tr class="odd">
     <th>Situation</th>
     <th>Result</th>
   </tr>
   <tr class="even">
     <td><a href="driving.html" title="Driving">Driving</a></td>
     <td>Busy</td>
   </tr>
 </table>
            

Parameters:
$text - a sequence of valid JSON strings
Returns:

a sequence of nodes that result from parsing the given JSON strings according to the JsonML mapping

Errors
API0063 if the string representation of the value passed was empty or any of the passed strings are not valid JSON

serialize-ml
declare function json:serialize-ml (
$xml as node()*
) as xs:string* external

This function serializes a sequence of elements by transforming each element into a valid JSON string according to the JsonML mapping described above.

Query:

              
 json:serialize-ml(
   <ul>
     <li>true</li>
     <li href="driving.html" title="Driving">Second item</li>
     <li/>
     <li>-1.4</li>
   </ul>
 )
            

Result:

              
 ["ul",
   ["li", "true"],
   ["li", {"href":"driving.html"}, {"title":"Driving"}, "Second item"],
   ["li"],
   ["li", "-1.4"]
 ]
            

Parameters:
$xml - the sequence of nodes that should be converted to JsonML
Returns:

a sequence of JSON strings resulting from transforming the given parameter to JSON according to the XDM-JsonML mapping