Product SiteDocumentation Site

Chapter 9. Combining XML and JSON

JSONiq is designed to allow XML and JSON to be used in the same query.
The syntax of JSONiq allows JSON constructors to contain XML values, and allows JSON constructors to occur in XML constructors. JSON does not support XML nodes or types, and XML does not support Objects or Arrays, but JSONiq allows Objects and Arrays to contain XML nodes, and defines rules for using JSONiq nodes in XML content expressions.
Example 9.1. XML in JSON
Both XML nodes and atomic values may occur in the values of Objects.
        {
          "element" : <mercury>Hg</mercury>,
          "atomic value" : xs:date("1896-01-24")
          "several dates" : [ xs:date("1066-10-14"), xs:date("1935-01-11"), xs:date("1989-11-09") ]
        }
XML nodes and atomic values may also appear in Arrays.
[ xs:date("1066-10-14"), <mercury>Hg</mercury>, "ice cream" ]

JSONiq does not allow XML nodes to contain Objects and Arrays. If an XQuery element content sequence, the value of the enclosed expression of an attribute, or the value of the content expression of a computed constructor contains an Object or Array, an error jerr:JNTY0011 is raised.
Example 9.2. Objects in XML Constructors
Objects can be indirectly used in the content expression of any XQuery constructor.
Query:
        let $object := { "x" : 10, "y" : 12 }
        let $x := $object("x")
        return <x>{ $x }</x>
An Array can also be used in the content expression.[7]:
        <svg><polygon stroke="blue" points="(jnlib:flatten([ [10,10], [20,10], [20,20], [10,20] ])}" /></svg>
Here is the result of the above query:
<svg><polygon stroke="blue" points="10 10 20 10 20 20 10 20" /></svg>



[7] The data in this example is taken from an example on Stefan Goessner's JSONT site (http://goessner.net/articles/jsont/).