Merge lp:~zorba-coders/zorba/updated-jsoniq-tutorials into lp:zorba

Proposed by Ghislain Fourny on 2012-07-25
Status: Superseded
Proposed branch: lp:~zorba-coders/zorba/updated-jsoniq-tutorials
Merge into: lp:zorba
Diff against target: 17 lines (+2/-2)
2 files modified
doc/zorba/JSONforXQuery.html (+1/-1)
doc/zorba/XQueryforJSON.html (+1/-1)
To merge this branch: bzr merge lp:~zorba-coders/zorba/updated-jsoniq-tutorials
Reviewer Review Type Date Requested Status
William Candillon 2012-07-25 Approve on 2012-08-17
Matthias Brantner 2012-07-25 Approve on 2012-07-25
Review via email: mp+116644@code.launchpad.net

Commit message

Updated JSONiq tutorial.

Description of the change

Updated JSONiq tutorial.

To post a comment you must log in.
review: Approve
review: Approve
Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job updated-jsoniq-tutorials-2012-08-20T12-57-06.122Z is finished. The final status was:

All tests succeeded!

10952. By Ghislain Fourny on 2012-08-20

Updated tutorials.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'doc/zorba/JSONforXQuery.html'
2--- doc/zorba/JSONforXQuery.html 2012-02-09 18:10:20 +0000
3+++ doc/zorba/JSONforXQuery.html 2012-07-25 13:53:23 +0000
4@@ -1,1 +1,1 @@
5-<html><head><title>JSON for XQuery</title><style type="text/css">@import url('https://themes.googleusercontent.com/fonts/css?kit=lhDjYqiy3mZ0x6ROQEUoUw');ol{margin:0;padding:0}p{margin:0}.c22{max-width:468pt;background-color:#ffffff;padding:72pt 72pt 72pt 72pt}.c19{list-style-type:disc;margin:0;padding:0}.c6{padding-left:0pt;text-align:justify;margin-left:36pt}.c0{height:11pt;direction:ltr}.c3{color:#38761d;font-family:Consolas}.c2{color:inherit;text-decoration:inherit}.c4{color:#1155cc;text-decoration:underline}.c1{direction:ltr}.c20{height:18pt}.c9{margin-left:58.5pt}.c13{text-indent:139.5pt}.c17{text-indent:144pt}.c7{margin-left:144pt}.c8{text-align:right}.c11{font-weight:bold}.c16{text-align:justify}.c10{margin-left:36pt}.c5{font-family:Consolas}.c18{color:#38761d}.c15{margin-left:49.5pt}.c21{text-align:center}.c23{color:#4a86e8}.c12{font-style:italic}.c14{margin-left:18pt}.title{padding-top:24pt;line-height:1.15;text-align:center;color:#4a86e8;font-size:36pt;font-family:Arial;font-weight:bold;padding-bottom:6pt}.subtitle{padding-top:18pt;line-height:1.15;text-align:left;color:#666666;font-style:italic;font-size:24pt;font-family:Georgia;padding-bottom:4pt}body{color:#000000;font-size:11pt;font-family:Arial}h1{padding-top:24pt;line-height:1.15;text-align:left;color:#4a86e8;font-size:24pt;font-family:Arial;font-weight:bold;padding-bottom:6pt}h2{padding-top:18pt;line-height:1.15;text-align:left;color:#ff9900;font-size:18pt;font-family:Arial;font-weight:bold;padding-bottom:4pt}h3{padding-top:14pt;line-height:1.15;text-align:left;color:#000000;font-size:14pt;font-family:Arial;font-weight:bold;padding-bottom:4pt}h4{padding-top:12pt;line-height:1.15;text-align:left;color:#000000;font-size:12pt;font-family:Arial;font-weight:bold;padding-bottom:2pt}h5{padding-top:11pt;line-height:1.15;text-align:left;color:#000000;font-size:11pt;font-family:Arial;font-weight:bold;padding-bottom:2pt}h6{padding-top:10pt;line-height:1.15;text-align:left;color:#000000;font-size:10pt;font-family:Arial;font-weight:bold;padding-bottom:2pt}</style></head><body class="c22"><p class="c1 title"><a name="h.li2wdq8unbq"></a><span>JSONiq for XQuery users</span></p><p class="c1 title"><a name="h.5ipqqclfxk4n"></a><span class="c18">JSON for XQuery</span></p><hr><p class="c1 c21"><span>This tutorial introduces the JSONiq language, which declaratively manipulates JSON data.</span></p><p class="c1 c21"><span>Why don&#39;t you go ahead can try the queries of this document on our online demo interface?</span></p><p class="c1 c21"><span>&nbsp;</span><span class="c4 c11"><a class="c2" href="http://jsoniq.zorba-xquery.com/">http://jsoniq.zorba-xquery.com/</a></span></p><hr><p class="c0"><span></span></p><h2 class="c1 c20"><a name="h.8gra0ylxutrv"></a></h2><p class="c1 c14"><span class="c4"><a class="c2" href="#h.f453h84m2u1a">JSON</a></span></p><p class="c1 c14"><span class="c4"><a class="c2" href="#h.qtfakecy3fr7">Elevator Pitch</a></span></p><p class="c1 c14"><span class="c4"><a class="c2" href="#h.yugk1mszt0re">And here you go</a></span></p><p class="c1 c10"><span class="c4"><a class="c2" href="#h.39rm37noeh9g">JSONiq types</a></span></p><p class="c1 c10"><span class="c4"><a class="c2" href="#h.j0wg8654ivtw">JSON Constructors</a></span></p><p class="c1 c10"><span class="c4"><a class="c2" href="#h.qe57mgdf5ql">JSON as a subset of JSONiq</a></span></p><p class="c1 c14"><span class="c4"><a class="c2" href="#h.mjv7kyjurri2">JSON Navigation</a></span></p><p class="c1 c10"><span class="c4"><a class="c2" href="#h.xosiyb17dh24">Objects</a></span></p><p class="c1 c10"><span class="c4"><a class="c2" href="#h.mrf42gizl3y6">Arrays</a></span></p><p class="c1 c10"><span class="c4"><a class="c2" href="#h.hdc3dgohtdut">Pairs</a></span></p><p class="c1 c14"><span class="c4"><a class="c2" href="#h.eb0bxle2oykl">Relational Algebra</a></span></p><p class="c1 c14"><span class="c4"><a class="c2" href="#h.oii6pvliilhi">Access external data</a></span></p><p class="c1 c14"><span class="c4"><a class="c2" href="#h.bm3buqmzhtko">I want more</a></span></p><p><span></span></p><p class="c0"><span></span></p><h1 class="c1"><a name="h.f453h84m2u1a"></a><span>JSON</span></h1><p class="c1 c16"><span>As explained on </span><span class="c4"><a class="c2" href="http://www.json.org/">http://www.json.org/</a></span><span>, JSON is a lightweight data-interchange format designed for humans as well as for computers. It supports as values:</span></p><ol class="c19" start="1"><li class="c6 c1"><span>objects (string-to-value map)</span></li><li class="c6 c1"><span>arrays (ordered sequence of values)</span></li><li class="c6 c1"><span>strings</span></li><li class="c6 c1"><span>numbers</span></li><li class="c6 c1"><span>booleans (true, false)</span></li><li class="c6 c1"><span>null</span></li></ol><p class="c0"><span></span></p><p class="c1"><span>JSONiq extends XQuery to query and update JSON data, like XML data.</span></p><h1 class="c1"><a name="h.qtfakecy3fr7"></a><span>Elevator Pitch</span></h1><p class="c1 c16"><span>Here is an appetizer before we start the tutorial from scratch.</span></p><p class="c0"><span></span></p><p class="c1"><span class="c5">let $stores :=</span></p><p class="c1"><span class="c5">[</span></p><p class="c1"><span class="c5">&nbsp; { &quot;store number&quot; : 1, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c1"><span class="c5">&nbsp; { &quot;store number&quot; : 2, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c1"><span class="c5">&nbsp; { &quot;store number&quot; : 3, &quot;state&quot; : &quot;CA&quot; },</span></p><p class="c1"><span class="c5">&nbsp; { &quot;store number&quot; : 4, &quot;state&quot; : &quot;CA&quot; }</span></p><p class="c1"><span class="c5">]</span></p><p class="c1"><span class="c5">let $sales := [</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;broiler&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 20 &nbsp;},</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 100 },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 50 },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 50 },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 100 },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 150 },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 500 },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 10 },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;shirt&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 10 }</span></p><p class="c1"><span class="c5">]</span></p><p class="c1"><span class="c5">let $join :=</span></p><p class="c1"><span class="c5">&nbsp; for $store in jn:values($stores), $sale in jn:values($sales)</span></p><p class="c1"><span class="c5">&nbsp; where $store(&quot;store number&quot;) = $sale(&quot;store number&quot;)</span></p><p class="c1"><span class="c5">&nbsp; return {</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; &quot;nb&quot; : $store(&quot;store number&quot;),</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; &quot;state&quot; : $store(&quot;state&quot;),</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; &quot;sold&quot; : $sale(&quot;product&quot;)</span></p><p class="c1"><span class="c5">&nbsp; }</span></p><p class="c1"><span class="c5">return [$join]</span></p><p class="c1 c7"><span class="c3">[ </span></p><p class="c1 c7"><span class="c3">&nbsp; { &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;broiler&quot; },</span></p><p class="c1 c7"><span class="c3">&nbsp; { &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c1 c7"><span class="c3">&nbsp; { &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c1 c7"><span class="c3">&nbsp; { &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c1 c7"><span class="c3">&nbsp; { &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c1 c7"><span class="c3">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c1 c7"><span class="c3">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c1 c7"><span class="c3">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c1 c7"><span class="c3">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;shirt&quot; }</span></p><p class="c1 c7"><span class="c3">&nbsp;]</span></p><p class="c0"><span></span></p><h1 class="c1"><a name="h.yugk1mszt0re"></a><span>And here you go</span></h1><h2 class="c1"><a name="h.39rm37noeh9g"></a><span>JSONiq types</span></h2><p class="c1"><span>JSONiq maps JSON types to XQuery. Numbers are xs:integer or xs:decimal, strings are xs:string, true and false are xs:boolean and null is a new atomic type jn:null.</span></p><p class="c0"><span></span></p><p class="c1"><span>JSONiq introduces new items: objects, arrays and pairs. Objects have pairs. Pairs have a string name and a value. Array have members which are values. Values are objects, arrays, XML nodes or atomic items.</span></p><p class="c0"><span></span></p><p class="c1"><span>The new item types for objects and arrays are object() and array(). json-item() is a supertype of both, and json-pair() is the item type for pairs.</span></p><h2 class="c1"><a name="h.j0wg8654ivtw"></a><span>JSON Constructors</span></h2><p class="c1"><span>JSONiq introduces JSON constructors, in a similar way to XML constructors.</span></p><p class="c0"><span></span></p><p class="c1"><span>You can put any expression in a array. The items in the sequence produced by the expression will become members of the array:</span></p><p class="c0"><span></span></p><p class="c1"><span class="c5">[ 1 to 10 ]</span></p><p class="c1 c8"><span class="c3">[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]</span></p><p class="c0 c8"><span class="c3"></span></p><p class="c1"><span>Or you can dynamically compute an object:</span></p><p class="c0"><span></span></p><p class="c1"><span class="c5">{</span></p><p class="c1"><span class="c5">&nbsp; &quot;Greeting&quot; : (let $d := &quot;Mister Spock&quot;</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return concat(&quot;Hello, &quot;, $d)),</span></p><p class="c1"><span class="c5">&nbsp; &quot;Farewell&quot; : string-join((&quot;Live&quot;, &quot;long&quot;, &quot;and&quot;, &quot;prosper&quot;),</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot; &quot;)</span></p><p class="c1"><span class="c5">}</span></p><p class="c0"><span class="c3"></span></p><p class="c1 c8"><span class="c3">{ &quot;Greeting&quot; : &quot;Hello, Mister Spock&quot;, &quot;Farewell&quot; : &quot;Live long and prosper&quot; }</span></p><p class="c0"><span class="c18"></span></p><p class="c1"><span>You can also dynamically generate pairs:</span></p><p class="c0"><span class="c5"></span></p><p class="c1"><span class="c5">concat(&quot;Integer &quot;, 2) : 2 * 2</span></p><p class="c1 c8"><span class="c3">&quot;Integer 2&quot; : 4</span></p><p class="c0 c8"><span class="c3"></span></p><p class="c1"><span>and then wrap lots of them in an object:</span></p><p class="c0"><span class="c5"></span></p><p class="c1"><span class="c5">{</span></p><p class="c1"><span class="c5">&nbsp; for $i in 1 to 10</span></p><p class="c1"><span class="c5">&nbsp; return concat(&quot;Square of &quot;, $i) : $i * $i</span></p><p class="c1"><span class="c5">}</span></p><p class="c1 c7"><span class="c3">{</span></p><p class="c1 c7"><span class="c3">&quot;Square of 1&quot; : 1,</span></p><p class="c1 c7"><span class="c3">&quot;Square of 2&quot; : 4,</span></p><p class="c1 c7"><span class="c3">&quot;Square of 3&quot; : 9,</span></p><p class="c1 c7"><span class="c3">&quot;Square of 4&quot; : 16,</span></p><p class="c1 c7"><span class="c3">&quot;Square of 5&quot; : 25,</span></p><p class="c1 c7"><span class="c3">&quot;Square of 6&quot; : 36,</span></p><p class="c1 c7"><span class="c3">&quot;Square of 7&quot; : 49,</span></p><p class="c1 c7"><span class="c3">&quot;Square of 8&quot; : 64,</span></p><p class="c1 c7"><span class="c3">&quot;Square of 9&quot; : 81,</span></p><p class="c1 c7"><span class="c3">&quot;Square of 10&quot; : 100</span></p><p class="c1 c7"><span class="c3">}</span></p><p class="c0"><span></span></p><h2 class="c1"><a name="h.qe57mgdf5ql"></a><span>JSON as a subset of JSONiq</span></h2><p class="c1 c16"><span>Every well-formed JSON document is a JSONiq query as well. This means that you can copy-and-paste a JSON document into a query. The following are JSONiq queries that are &quot;idempotent&quot; (they just output themselves):</span></p><p class="c0"><span></span></p><p class="c1"><span class="c5">{ &quot;pi&quot; : 3.14, &quot;sq2&quot; : 1.4 }</span></p><p class="c1 c8"><span class="c3">{ &quot;pi&quot; : 3.14, &quot;sq2&quot; : 1.4 }</span></p><p class="c0"><span class="c3"></span></p><p class="c1"><span class="c5">[ 2, 3, 5, 7, 11, 13 ]</span></p><p class="c1 c8"><span class="c3">[ 2, 3, 5, 7, 11, 13 ]</span></p><p class="c0"><span class="c5"></span></p><p class="c1"><span class="c5">{</span></p><p class="c1"><span class="c5">&nbsp; &quot;operations&quot; : [</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; { &quot;binary&quot; : [ &quot;and&quot;, &quot;or&quot;] },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; { &quot;unary&quot; : [&quot;not&quot;] }</span></p><p class="c1"><span class="c5">&nbsp; ],</span></p><p class="c1"><span class="c5">&nbsp; &quot;bits&quot; : [</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; 0, 1</span></p><p class="c1"><span class="c5">&nbsp; ]</span></p><p class="c1"><span class="c5">}</span></p><p class="c1 c17"><span class="c3">{</span></p><p class="c1 c17"><span class="c3">&nbsp; &quot;operations&quot; : [</span></p><p class="c1 c17"><span class="c3">&nbsp; &nbsp; { &quot;binary&quot; : [ &quot;and&quot;, &quot;or&quot; ] },</span></p><p class="c1 c17"><span class="c3">&nbsp; &nbsp; { &quot;unary&quot; : [ &quot;not&quot; ] }</span></p><p class="c1 c17"><span class="c3">&nbsp; ],</span></p><p class="c1 c17"><span class="c3">&nbsp; &quot;bits&quot; : [</span></p><p class="c1 c17"><span class="c3">&nbsp; &nbsp; 0, 1</span></p><p class="c1 c17"><span class="c3">&nbsp; ]</span></p><p class="c1 c17"><span class="c3">}</span></p><p class="c0 c8"><span class="c3"></span></p><p class="c1 c16"><span>This works with objects, arrays (even nested), strings, numbers, booleans, null. </span><span class="c12">The exceptions to this rule (but we are working on it!) are that:</span></p><p class="c1 c16"><span class="c12">(i) if a pair has a true, false or null value, there must be a space on at least one side of the colon (this will be fixed).</span></p><p class="c1 c16"><span class="c12">(ii) empty objects are not recognized</span></p><p class="c1 c16"><span class="c12">(iii) characters escaped with the \ in JSON strings are not recognized</span></p><p class="c0"><span class="c5 c12"></span></p><p class="c1 c16"><span>It also works the other way round: if your query outputs an object or an array, you can readily use it as a JSON document.</span></p><p class="c0 c8"><span class="c3"></span></p><h1 class="c1"><a name="h.mjv7kyjurri2"></a><span>JSON Navigation</span></h1><p class="c1 c16"><span>I</span><span>f you have some JSON data, you can access it and navigate.</span></p><p class="c0 c16"><span></span></p><p class="c1 c16"><span>All you need to know is, again: JSONiq views</span></p><ol class="c19" start="1"><li class="c1 c6"><span>an array as a sequence of values,</span></li><li class="c6 c1"><span>an object as a set of pairs,</span></li><li class="c6 c1"><span>a pair as a (name, value) tuple.</span></li></ol><h2 class="c1 c16"><a name="h.xosiyb17dh24"></a><span>Objects</span></h2><p class="c1 c16"><span>If you use an object as a functor and provide it with a string, it will return the pair named after its parameter:</span></p><p class="c0"><span class="c5"></span></p><p class="c1"><span class="c5">let $person := {</span></p><p class="c1"><span class="c5">&nbsp; &quot;first name&quot; : &quot;Sarah&quot;,</span></p><p class="c1"><span class="c5">&nbsp; &quot;age&quot; : 13,</span></p><p class="c1"><span class="c5">&nbsp; &quot;gender&quot; : &quot;female&quot;,</span></p><p class="c1"><span class="c5">&nbsp; &quot;friends&quot; : [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c1"><span class="c5">}</span></p><p class="c1"><span class="c5">return $person(&quot;first name&quot;)</span></p><p class="c1 c8"><span class="c3">&quot;first name&quot; : &quot;Sarah&quot;</span></p><p class="c0 c8"><span class="c3"></span></p><h2 class="c1"><a name="h.mrf42gizl3y6"></a><span>Arrays</span></h2><p class="c1 c16"><span>If you use an array as a functor and provide it with an integer, it will return the corresponding entry:</span></p><p class="c0"><span class="c5"></span></p><p class="c1"><span class="c5">let $friends := [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c1"><span class="c5">return $friends(2)</span></p><p class="c1 c8"><span class="c3">Mary</span></p><h2 class="c1"><a name="h.hdc3dgohtdut"></a><span>Pairs</span></h2><p class="c1"><span>Given a pair, you can get its name and value using the functions jn:name and jn:value:</span></p><p class="c0"><span></span></p><p class="c1"><span class="c5">let $pair := &quot;name of the pair&quot; : &quot;value of the pair&quot;</span></p><p class="c1"><span class="c5">return { &nbsp;&quot;name&quot; : jn:name($pair), &quot;value&quot; : jn:value($pair) }</span></p><p class="c0"><span class="c5"></span></p><p class="c1 c8"><span class="c3">{ &quot;name&quot; : &quot;name of the pair&quot;, &quot;value&quot; : &quot;value of the pair&quot; }</span></p><p class="c0"><span class="c18"></span></p><p class="c1"><span>This functions also exist in plural form for objects:</span></p><p class="c0"><span></span></p><p class="c1"><span class="c5">let $person := {</span></p><p class="c1"><span class="c5">&nbsp; &quot;name&quot; : &quot;Sarah&quot;,</span></p><p class="c1"><span class="c5">&nbsp; &quot;age&quot; : 13,</span></p><p class="c1"><span class="c5">&nbsp; &quot;gender&quot; : &quot;female&quot;,</span></p><p class="c1"><span class="c5">&nbsp; &quot;friends&quot; : [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c1"><span class="c5">}</span></p><p class="c1"><span class="c5">return { &quot;names&quot; : [ jn:names($person)],</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;values&quot; : [ jn:values($person)]</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; &nbsp; &nbsp;}</span></p><p class="c1 c9"><span class="c3">{</span></p><p class="c1 c9"><span class="c3">&quot;names&quot; : [ &quot;name&quot;, &quot;age&quot;, &quot;gender&quot;, &quot;friends&quot; ],</span></p><p class="c1 c9"><span class="c3">&quot;values&quot; : [ &quot;Sarah&quot;, 13, &quot;female&quot;, [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot; ] ]</span></p><p class="c1 c9"><span class="c3">}</span></p><p class="c1"><span>jn:values can also be used for an array:</span></p><p class="c0"><span></span></p><p class="c1"><span class="c5">let $person := {</span></p><p class="c1"><span class="c5">&nbsp; &quot;name&quot; : &quot;Sarah&quot;,</span></p><p class="c1"><span class="c5">&nbsp; &quot;age&quot; : 13,</span></p><p class="c1"><span class="c5">&nbsp; &quot;gender&quot; : &quot;female&quot;,</span></p><p class="c1"><span class="c5">&nbsp; &quot;friends&quot; : [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c1"><span class="c5">}</span></p><p class="c1"><span class="c5">return jn:values($person(&quot;friends&quot;))</span></p><p class="c1 c8"><span class="c18">[ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot; ]</span></p><p class="c0 c8"><span class="c18"></span></p><h1 class="c1"><a name="h.eb0bxle2oykl"></a><span>Relational Algebra</span></h1><p class="c1 c16"><span>Remember last century&#39;s SELECT FROM WHERE statements? Well, JSONiq didn&#39;t throw out the baby with the bathwater. It has selection, projection and join capability.</span></p><p class="c0"><span></span></p><p class="c1"><span class="c5">let $stores :=</span></p><p class="c1"><span class="c5">[</span></p><p class="c1"><span class="c5">&nbsp; { &quot;store number&quot; : 1, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c1"><span class="c5">&nbsp; { &quot;store number&quot; : 2, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c1"><span class="c5">&nbsp; { &quot;store number&quot; : 3, &quot;state&quot; : &quot;CA&quot; },</span></p><p class="c1"><span class="c5">&nbsp; { &quot;store number&quot; : 4, &quot;state&quot; : &quot;CA&quot; }</span></p><p class="c1"><span class="c5">]</span></p><p class="c1"><span class="c5">let $sales := [</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;broiler&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 20 &nbsp;},</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 100 },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 50 },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 50 },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 100 },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 150 },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 500 },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 10 },</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;{ &quot;product&quot; : &quot;shirt&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 10 }</span></p><p class="c1"><span class="c5">]</span></p><p class="c1"><span class="c5">let $join :=</span></p><p class="c1"><span class="c5">&nbsp; for $store in jn:values($stores), $sale in jn:values($sales)</span></p><p class="c1"><span class="c5">&nbsp; where $store(&quot;store number&quot;) = $sale(&quot;store number&quot;)</span></p><p class="c1"><span class="c5">&nbsp; return {</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; &quot;nb&quot; : $store(&quot;store number&quot;),</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; &quot;state&quot; : $store(&quot;state&quot;),</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; &quot;sold&quot; : $sale(&quot;product&quot;)</span></p><p class="c1"><span class="c5">&nbsp; }</span></p><p class="c1"><span class="c5">return [$join]</span></p><p class="c1 c13"><span class="c3">[ </span></p><p class="c1 c13"><span class="c3">{ &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;broiler&quot; },</span></p><p class="c1 c13"><span class="c3">{ &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c1 c13"><span class="c3">{ &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c1 c13"><span class="c3">{ &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c1 c13"><span class="c3">{ &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c1 c13"><span class="c3">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c1 c13"><span class="c3">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c1 c13"><span class="c3">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c1 c13"><span class="c3">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;shirt&quot; }</span></p><p class="c1 c13"><span class="c3">&nbsp;]</span></p><h1 class="c1"><a name="h.oii6pvliilhi"></a><span>Access external data</span></h1><p class="c1"><span>Our implementation supports collections of JSON objects or arrays:</span></p><p class="c0"><span></span></p><p class="c1"><span class="c5">dml:collection(&quot;my:data&quot;)</span></p><p class="c1 c8"><span class="c3">{ &quot;foo&quot; : &quot;Your&quot; }</span></p><p class="c1 c8"><span class="c3">{ &quot;foo&quot; : &quot;Collection&quot; }</span></p><p class="c1 c8"><span class="c3">{ &quot;foo&quot; : &quot;of&quot; }</span></p><p class="c1 c8"><span class="c3">{ &quot;foo&quot; : &quot;JSON&quot; }</span></p><p class="c1 c8"><span class="c3">{ &quot;foo&quot; : &quot;objects&quot; }</span></p><p class="c0 c8"><span class="c3"></span></p><p class="c1"><span>It is also possible to get JSON content with an HTTP request, or by parsing it from a string. Functions that handle this are available.</span></p><p class="c0"><span class="c3"></span></p><h1 class="c1"><a name="h.fopcxnkoydgt"></a><span>JSON and XML</span></h1><p class="c1 c16"><span>You can readily use XML and JSON in the same program. If you put a JSON array in an XML constructor, it will be flattened (its member values are recursively taken, even in arrays of arrays). If you put a JSON pair in an XML constructor, it will be unboxed (its value is taken).</span></p><p class="c0"><span></span></p><p class="c1"><span class="c5">let $data := {</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;&quot;color&quot; : &quot;blue&quot;,</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;&quot;closed&quot; : true,</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;&quot;points&quot; : [[10,10], [20,10], [20,20], [10,20]]</span></p><p class="c1"><span class="c5">&nbsp; &nbsp;}</span></p><p class="c0"><span class="c5"></span></p><p class="c1"><span class="c5">let $stroke := attribute stroke { $data(&quot;color&quot;) }</span></p><p class="c1"><span class="c5">let $points := attribute points { $data(&quot;points&quot;) }</span></p><p class="c1"><span class="c5">return</span></p><p class="c1"><span class="c5">&nbsp; if ($data(&quot;closed&quot;)) then</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; &lt;svg&gt;&lt;polygon&gt;{ $stroke, $points }&lt;/polygon&gt;&lt;/svg&gt;</span></p><p class="c1"><span class="c5">&nbsp; else</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; &lt;svg&gt;&lt;polyline&gt;{ $stroke, $points }&lt;/polyline&gt;&lt;/svg&gt;</span></p><p class="c0"><span class="c5"></span></p><p class="c1 c15"><span class="c3">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></p><p class="c1 c15"><span class="c3">&lt;svg&gt;&lt;polygon stroke=&quot;blue&quot; points=&quot;10 10 20 10 20 20 10 20&quot;/&gt;&lt;/svg&gt;</span></p><p class="c0"><span></span></p><h1 class="c1"><a name="h.bm3buqmzhtko"></a><span>I want more</span></h1><p class="c1 c16"><span>JSONiq supports JSON updates. You can declaratively update your JSON data. JSONiq provides functions that produce a list of updates. The list of updates that is eventually output by your program is then applied to your JSON data.</span></p><p class="c0"><span></span></p><p class="c1"><span class="c5">let $my-data := jn:json(&quot;persons.json&quot;)</span></p><p class="c1"><span class="c5">let $john := jn:value($my-data(&quot;John&quot;))</span></p><p class="c1"><span class="c5">let $mary := jn:value($my-data(&quot;Mary&quot;))</span></p><p class="c1"><span class="c5">return (jn:replace-value($john, &quot;status&quot;, &quot;married&quot;),</span></p><p class="c1"><span class="c5">&nbsp; &nbsp; &nbsp; &nbsp; j:replace-value($mary, &quot;status&quot;, &quot;married&quot;))</span></p><p class="c0"><span></span></p><p class="c1"><span>JSONiq works with the XQuery 3.0 standard (switch, typeswitch and try-catch expressions, universal/existential quantifiers, path expressions, filtering expressions, functors, mappings, grouping, windowing will work). The Zorba implementation is also compatible with the proprietary Zorba scripting.</span></p></body></html>
6\ No newline at end of file
7+<html><head><title>JSON for XQuery</title><style type="text/css">@import url('https://themes.googleusercontent.com/fonts/css?kit=lhDjYqiy3mZ0x6ROQEUoUw');ol{margin:0;padding:0}.c22{max-width:468pt;background-color:#ffffff;padding:72pt 72pt 72pt 72pt}.c7{padding-left:0pt;text-align:justify;margin-left:36pt}.c17{list-style-type:disc;margin:0;padding:0}.c23{text-indent:-13pt;margin-left:77pt}.c5{color:#1155cc;text-decoration:underline}.c13{color:inherit;text-decoration:inherit}.c18{font-weight:bold}.c6{margin-left:18pt}.c15{text-indent:139.5pt}.c8{color:#38761d}.c16{color:#4a86e8}.c14{font-style:italic}.c2{text-align:right}.c9{text-align:justify}.c4{margin-left:144pt}.c19{margin-left:36pt}.c11{text-indent:156pt}.c3{height:11pt}.c0{font-family:"Consolas"}.c1{direction:ltr}.c20{text-align:center}.c12{margin-left:49.5pt}.c21{height:18pt}.c10{text-indent:144pt}.title{padding-top:24pt;line-height:1.15;text-align:center;color:#4a86e8;font-size:36pt;font-family:"Arial";font-weight:bold;padding-bottom:6pt}.subtitle{padding-top:18pt;line-height:1.15;text-align:left;color:#666666;font-style:italic;font-size:24pt;font-family:"Georgia";padding-bottom:4pt}li{color:#000000;font-size:11pt;font-family:"Arial"}p{color:#000000;font-size:11pt;margin:0;font-family:"Arial"}h1{padding-top:24pt;line-height:1.15;text-align:left;color:#4a86e8;font-size:24pt;font-family:"Arial";font-weight:bold;padding-bottom:6pt}h2{padding-top:18pt;line-height:1.15;text-align:left;color:#ff9900;font-size:18pt;font-family:"Arial";font-weight:bold;padding-bottom:4pt}h3{padding-top:14pt;line-height:1.15;text-align:left;color:#000000;font-size:14pt;font-family:"Arial";font-weight:bold;padding-bottom:4pt}h4{padding-top:12pt;line-height:1.15;text-align:left;color:#000000;font-size:12pt;font-family:"Arial";font-weight:bold;padding-bottom:2pt}h5{padding-top:11pt;line-height:1.15;text-align:left;color:#000000;font-size:11pt;font-family:"Arial";font-weight:bold;padding-bottom:2pt}h6{padding-top:10pt;line-height:1.15;text-align:left;color:#000000;font-size:10pt;font-family:"Arial";font-weight:bold;padding-bottom:2pt}</style></head><body class="c22"><p class="c1 title"><a name="h.li2wdq8unbq"></a><span>JSONiq for XQuery users</span></p><p class="c1 title"><a name="h.5ipqqclfxk4n"></a><span class="c8">JSON for XQuery</span></p><hr><p class="c1 c20"><span>This tutorial introduces the JSONiq language, which declaratively manipulates JSON data.</span></p><p class="c1 c20"><span>Why don&#39;t you go ahead can try the queries of this document on our online demo interface?</span></p><p class="c1 c20"><span>&nbsp;</span><span class="c5 c18"><a class="c13" href="http://jsoniq.zorba-xquery.com/">http://jsoniq.zorba-xquery.com/</a></span></p><hr><p class="c3 c1"><span></span></p><h2 class="c1 c21"><a name="h.8gra0ylxutrv"></a></h2><p class="c6 c1"><span class="c5"><a class="c13" href="#h.f453h84m2u1a">JSON</a></span></p><p class="c6 c1"><span class="c5"><a class="c13" href="#h.qtfakecy3fr7">Elevator Pitch</a></span></p><p class="c6 c1"><span class="c5"><a class="c13" href="#h.yugk1mszt0re">And here you go</a></span></p><p class="c19 c1"><span class="c5"><a class="c13" href="#h.39rm37noeh9g">JSONiq types</a></span></p><p class="c1 c19"><span class="c5"><a class="c13" href="#h.j0wg8654ivtw">JSON Constructors</a></span></p><p class="c19 c1"><span class="c5"><a class="c13" href="#h.qe57mgdf5ql">JSON as a subset of JSONiq</a></span></p><p class="c1 c6"><span class="c5"><a class="c13" href="#h.dewn8sw3husv">JSON Navigation</a></span></p><p class="c19 c1"><span class="c5"><a class="c13" href="#h.zcnazhl6374z">Objects</a></span></p><p class="c19 c1"><span class="c5"><a class="c13" href="#h.30wafxvenhgy">Arrays</a></span></p><p class="c6 c1"><span class="c5"><a class="c13" href="#h.eb0bxle2oykl">Relational Algebra</a></span></p><p class="c6 c1"><span class="c5"><a class="c13" href="#h.oii6pvliilhi">Access external data</a></span></p><p class="c6 c1"><span class="c5"><a class="c13" href="#h.fopcxnkoydgt">JSON and XML</a></span></p><p class="c6 c1"><span class="c5"><a class="c13" href="#h.bm3buqmzhtko">I want more</a></span></p><p class="c3 c1"><span></span></p><h1 class="c1"><a name="h.f453h84m2u1a"></a><span>JSON</span></h1><p class="c9 c1"><span>As explained on </span><span class="c5"><a class="c13" href="http://www.json.org/">http://www.json.org/</a></span><span>, JSON is a lightweight data-interchange format designed for humans as well as for computers. It supports as values:</span></p><ol class="c17" start="1"><li class="c7 c1"><span>objects (string-to-value map)</span></li><li class="c7 c1"><span>arrays (ordered sequence of values)</span></li><li class="c7 c1"><span>strings</span></li><li class="c7 c1"><span>numbers</span></li><li class="c7 c1"><span>booleans (true, false)</span></li><li class="c7 c1"><span>null</span></li></ol><p class="c3 c1"><span></span></p><p class="c1"><span>JSONiq extends XQuery to query and update JSON data, like XML data.</span></p><h1 class="c1"><a name="h.qtfakecy3fr7"></a><span>Elevator Pitch</span></h1><p class="c9 c1"><span>Here is an appetizer before we start the tutorial from scratch.</span></p><p class="c3 c1"><span></span></p><p class="c1"><span class="c0">let $stores :=</span></p><p class="c1"><span class="c0">[</span></p><p class="c1"><span class="c0">&nbsp; { &quot;store number&quot; : 1, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c1"><span class="c0">&nbsp; { &quot;store number&quot; : 2, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c1"><span class="c0">&nbsp; { &quot;store number&quot; : 3, &quot;state&quot; : &quot;CA&quot; },</span></p><p class="c1"><span class="c0">&nbsp; { &quot;store number&quot; : 4, &quot;state&quot; : &quot;CA&quot; }</span></p><p class="c1"><span class="c0">]</span></p><p class="c1"><span class="c0">let $sales := [</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;broiler&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 20 &nbsp;},</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 100 },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 50 },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 50 },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 100 },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 150 },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 500 },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 10 },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;shirt&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 10 }</span></p><p class="c1"><span class="c0">]</span></p><p class="c1"><span class="c0">let $join :=</span></p><p class="c1"><span class="c0">&nbsp; for $store in jn:members($stores), $sale in jn:members($sales)</span></p><p class="c1"><span class="c0">&nbsp; where $store(&quot;store number&quot;) = $sale(&quot;store number&quot;)</span></p><p class="c1"><span class="c0">&nbsp; return {</span></p><p class="c1"><span class="c0">&nbsp; &nbsp; &quot;nb&quot; : $store(&quot;store number&quot;),</span></p><p class="c1"><span class="c0">&nbsp; &nbsp; &quot;state&quot; : $store(&quot;state&quot;),</span></p><p class="c1"><span class="c0">&nbsp; &nbsp; &quot;sold&quot; : $sale(&quot;product&quot;)</span></p><p class="c1"><span class="c0">&nbsp; }</span></p><p class="c1"><span class="c0">return [$join]</span></p><p class="c4 c1"><span class="c8 c0">[ </span></p><p class="c4 c1"><span class="c8 c0">&nbsp; { &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;broiler&quot; },</span></p><p class="c4 c1"><span class="c8 c0">&nbsp; { &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c4 c1"><span class="c8 c0">&nbsp; { &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c4 c1"><span class="c0 c8">&nbsp; { &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c4 c1"><span class="c8 c0">&nbsp; { &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c4 c1"><span class="c8 c0">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c4 c1"><span class="c8 c0">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c4 c1"><span class="c8 c0">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c4 c1"><span class="c8 c0">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;shirt&quot; }</span></p><p class="c4 c1"><span class="c8 c0">&nbsp;]</span></p><p class="c3 c1"><span></span></p><h1 class="c1"><a name="h.yugk1mszt0re"></a><span>And here you go</span></h1><h2 class="c1"><a name="h.39rm37noeh9g"></a><span>JSONiq types</span></h2><p class="c1"><span>JSONiq maps JSON types to XQuery. Numbers are xs:integer or xs:decimal, strings are xs:string, true and false are xs:boolean and null is a new atomic type jn:null.</span></p><p class="c3 c1"><span></span></p><p class="c1"><span>By default, in JSONiq, for convenience, true, false and null are recognized as literals instead of name tests. However, this can be deactivated.</span></p><p class="c3 c1"><span></span></p><p class="c1"><span>JSONiq introduces new items: objects and arrays. Objects are sets of key/value pairs. Array have members which are values. Values are objects, arrays, XML nodes, functions or atomic items.</span></p><p class="c3 c1"><span></span></p><p class="c1"><span>The new item types for objects and arrays are object() and array(). json-item() is a supertype of both. structured-item() is a supertype for json-item() and node().</span></p><h2 class="c1"><a name="h.j0wg8654ivtw"></a><span>JSON Constructors</span></h2><p class="c1"><span>JSONiq introduces JSON constructors, in a similar way to XML constructors.</span></p><p class="c3 c1"><span></span></p><p class="c1"><span>You can put any expression in a array. The items in the sequence produced by the expression will become members of the array:</span></p><p class="c3 c1"><span></span></p><p class="c1"><span class="c0">[ 1 to 10 ]</span></p><p class="c2 c1"><span class="c8 c0">[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]</span></p><p class="c2 c3 c1"><span class="c8 c0"></span></p><p class="c1"><span>Or you can dynamically compute an object:</span></p><p class="c3 c1"><span></span></p><p class="c1"><span class="c0">{</span></p><p class="c1"><span class="c0">&nbsp; &quot;Greeting&quot; : (let $d := &quot;Mister Spock&quot;</span></p><p class="c1"><span class="c0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return concat(&quot;Hello, &quot;, $d)),</span></p><p class="c1"><span class="c0">&nbsp; &quot;Farewell&quot; : string-join((&quot;Live&quot;, &quot;long&quot;, &quot;and&quot;, &quot;prosper&quot;),</span></p><p class="c1"><span class="c0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot; &quot;)</span></p><p class="c1"><span class="c0">}</span></p><p class="c3 c1"><span class="c8 c0"></span></p><p class="c2 c1"><span class="c8 c0">{ &quot;Greeting&quot; : &quot;Hello, Mister Spock&quot;, &quot;Farewell&quot; : &quot;Live long and prosper&quot; }</span></p><p class="c3 c1"><span class="c8"></span></p><p class="c1"><span>You can also dynamically generate singleton objects:</span></p><p class="c3 c1"><span class="c0"></span></p><p class="c1"><span class="c0">{ concat(&quot;Integer &quot;, 2) : 2 * 2 }</span></p><p class="c2 c1"><span class="c8 c0">{ &quot;Integer 2&quot; : 4 }</span></p><p class="c2 c3 c1"><span class="c8 c0"></span></p><p class="c1"><span>and then wrap lots of them in an object:</span></p><p class="c3 c1"><span class="c0"></span></p><p class="c1"><span class="c0">{|</span></p><p class="c1"><span class="c0">&nbsp; for $i in 1 to 10</span></p><p class="c1"><span class="c0">&nbsp; return { concat(&quot;Square of &quot;, $i) : $i * $i }</span></p><p class="c1"><span class="c0">|}</span></p><p class="c4 c1"><span class="c8 c0">{</span></p><p class="c4 c1"><span class="c8 c0">&quot;Square of 1&quot; : 1,</span></p><p class="c1 c4"><span class="c8 c0">&quot;Square of 2&quot; : 4,</span></p><p class="c4 c1"><span class="c8 c0">&quot;Square of 3&quot; : 9,</span></p><p class="c4 c1"><span class="c8 c0">&quot;Square of 4&quot; : 16,</span></p><p class="c4 c1"><span class="c8 c0">&quot;Square of 5&quot; : 25,</span></p><p class="c4 c1"><span class="c8 c0">&quot;Square of 6&quot; : 36,</span></p><p class="c4 c1"><span class="c8 c0">&quot;Square of 7&quot; : 49,</span></p><p class="c4 c1"><span class="c8 c0">&quot;Square of 8&quot; : 64,</span></p><p class="c4 c1"><span class="c8 c0">&quot;Square of 9&quot; : 81,</span></p><p class="c4 c1"><span class="c8 c0">&quot;Square of 10&quot; : 100</span></p><p class="c4 c1"><span class="c8 c0">}</span></p><p class="c3 c1"><span></span></p><h2 class="c1"><a name="h.qe57mgdf5ql"></a><span>JSON as a subset of JSONiq</span></h2><p class="c9 c1"><span>As a rule of thumb, a well-formed JSON document is a JSONiq query as well. This means that you can copy-and-paste a JSON document into a query. The following are JSONiq queries that are &quot;idempotent&quot; (they just output themselves):</span></p><p class="c3 c1"><span></span></p><p class="c1"><span class="c0">{ &quot;pi&quot; : 3.14, &quot;sq2&quot; : 1.4 }</span></p><p class="c2 c1"><span class="c8 c0">{ &quot;pi&quot; : 3.14, &quot;sq2&quot; : 1.4 }</span></p><p class="c3 c1"><span class="c8 c0"></span></p><p class="c1"><span class="c0">[ 2, 3, 5, 7, 11, 13 ]</span></p><p class="c2 c1"><span class="c8 c0">[ 2, 3, 5, 7, 11, 13 ]</span></p><p class="c1 c3"><span class="c0"></span></p><p class="c1"><span class="c0">{</span></p><p class="c1"><span class="c0">&nbsp; &quot;operations&quot; : [</span></p><p class="c1"><span class="c0">&nbsp; &nbsp; { &quot;binary&quot; : [ &quot;and&quot;, &quot;or&quot;] },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp; { &quot;unary&quot; : [&quot;not&quot;] }</span></p><p class="c1"><span class="c0">&nbsp; ],</span></p><p class="c1"><span class="c0">&nbsp; &quot;bits&quot; : [</span></p><p class="c1"><span class="c0">&nbsp; &nbsp; 0, 1</span></p><p class="c1"><span class="c0">&nbsp; ]</span></p><p class="c1"><span class="c0">}</span></p><p class="c1 c10"><span class="c8 c0">{</span></p><p class="c1 c10"><span class="c8 c0">&nbsp; &quot;operations&quot; : [</span></p><p class="c1 c10"><span class="c8 c0">&nbsp; &nbsp; { &quot;binary&quot; : [ &quot;and&quot;, &quot;or&quot; ] },</span></p><p class="c1 c10"><span class="c8 c0">&nbsp; &nbsp; { &quot;unary&quot; : [ &quot;not&quot; ] }</span></p><p class="c1 c10"><span class="c8 c0">&nbsp; ],</span></p><p class="c1 c10"><span class="c8 c0">&nbsp; &quot;bits&quot; : [</span></p><p class="c1 c10"><span class="c8 c0">&nbsp; &nbsp; 0, 1</span></p><p class="c1 c10"><span class="c8 c0">&nbsp; ]</span></p><p class="c1 c10"><span class="c8 c0">}</span></p><p class="c2 c3 c1"><span class="c8 c0"></span></p><p class="c9 c1"><span>This works with objects, arrays (even nested), strings, numbers, booleans, null. </span><span class="c14">The exceptions to this rule (but we are working on it!) are that:</span></p><p class="c9 c1"><span class="c14">(i) if a pair has a true, false or null value, there must be a space on at least one side of the colon (this will be fixed soon).</span></p><p class="c9 c1"><span class="c14">(ii) empty objects are not recognized</span></p><p class="c9 c1"><span class="c14">(iii) characters escaped with the \ in JSON strings are not recognized</span></p><p class="c3 c1"><span class="c14 c0"></span></p><p class="c9 c1"><span>It also works the other way round: if your query outputs an object or an array, you can directly use it as a JSON document.</span></p><p class="c2 c3 c1"><span class="c8 c0"></span></p><h1 class="c1"><a name="h.dewn8sw3husv"></a><span>JSON Navigation</span></h1><p class="c9 c1"><span>Up to now, you learnt how to compose expressions so as to do some computations and to build objects and arrays. It also works the other way round: if you have some JSON data, you can access it and navigate.</span></p><p class="c11 c3 c1"><span></span></p><p class="c9 c1"><span>All you need to know is: JSONiq views</span></p><ol class="c17" start="1"><li class="c1 c7"><span>an array as a sequence of values,</span></li><li class="c7 c1"><span>an object as a set of name/value pairs</span></li></ol><h2 class="c1 c9"><a name="h.zcnazhl6374z"></a><span>Objects</span></h2><p class="c9 c1"><span>If you use an object as a functor and provide it with a string, it will return the value associated with its parameter:</span></p><p class="c11 c3 c1"><span></span></p><p class="c1"><span class="c0">let $person := {</span></p><p class="c1"><span class="c0">&nbsp;&quot;first name&quot; : &quot;Sarah&quot;,</span></p><p class="c1"><span class="c0">&nbsp;&quot;age&quot; : 13,</span></p><p class="c1"><span class="c0">&nbsp;&quot;gender&quot; : &quot;female&quot;,</span></p><p class="c1"><span class="c0">&nbsp;&quot;friends&quot; : [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c1"><span class="c0">}</span></p><p class="c1"><span class="c0">return $person(&quot;first name&quot;)</span></p><p class="c2 c11 c1"><span class="c8 c0">&quot;Sarah&quot;</span></p><p class="c11 c3 c1"><span class="c8 c0"></span></p><p class="c1"><span>You can also ask for all keys in an object:</span></p><p class="c3 c1"><span></span></p><p class="c1"><span class="c0">let $person := {</span></p><p class="c1"><span class="c0">&nbsp;&quot;name&quot; : &quot;Sarah&quot;,</span></p><p class="c1"><span class="c0">&nbsp;&quot;age&quot; : 13,</span></p><p class="c1"><span class="c0">&nbsp;&quot;gender&quot; : &quot;female&quot;,</span></p><p class="c1"><span class="c0">&nbsp;&quot;friends&quot; : [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c1"><span class="c0">}</span></p><p class="c1"><span class="c0">return { &quot;keys&quot; : [ jn:keys($person)] }</span></p><p class="c11 c3 c1"><span class="c0"></span></p><p class="c2 c1 c23"><span class="c8 c0">{ &quot;keys&quot; : [ &quot;name&quot;, &quot;age&quot;, &quot;gender&quot;, &quot;friends&quot; ] }</span></p><h2 class="c1"><a name="h.30wafxvenhgy"></a><span>Arrays</span></h2><p class="c9 c1"><span>If you use an array as a functor and provide it with an integer, it will return the corresponding entry:</span></p><p class="c11 c3 c1"><span></span></p><p class="c1"><span class="c0">let $friends := [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c1"><span class="c0">return $friends(2)</span></p><p class="c2 c11 c1"><span class="c8 c0">Mary</span></p><p class="c3 c1"><span class="c8 c0"></span></p><p class="c1"><span>It is also possible to get the size of an array:</span></p><p class="c11 c3 c1"><span></span></p><p class="c1"><span class="c0">let $person := {</span></p><p class="c1"><span class="c0">&nbsp;&quot;name&quot; : &quot;Sarah&quot;,</span></p><p class="c1"><span class="c0">&nbsp;&quot;age&quot; : 13,</span></p><p class="c1"><span class="c0">&nbsp;&quot;gender&quot; : &quot;female&quot;,</span></p><p class="c1"><span class="c0">&nbsp;&quot;friends&quot; : [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c1"><span class="c0">}</span></p><p class="c1"><span class="c0">return { &quot;how many friends&quot; : jn:size($person(&quot;friends&quot;)) }</span></p><p class="c11 c3 c1"><span class="c0"></span></p><p class="c2 c11 c1"><span class="c8 c0">{ &quot;how many friends&quot; : 3 }</span></p><p class="c11 c3 c1"><span class="c8 c0"></span></p><p class="c1"><span>For convenience, there is a function that returns all elements in an array, as a sequence:</span></p><p class="c11 c3 c1"><span></span></p><p class="c1"><span class="c0">let $person := {</span></p><p class="c1"><span class="c0">&nbsp;&quot;name&quot; : &quot;Sarah&quot;,</span></p><p class="c1"><span class="c0">&nbsp;&quot;age&quot; : 13,</span></p><p class="c1"><span class="c0">&nbsp;&quot;gender&quot; : &quot;female&quot;,</span></p><p class="c1"><span class="c0">&nbsp;&quot;friends&quot; : [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c1"><span class="c0">}</span></p><p class="c1"><span class="c0">return jn:members($person(&quot;friends&quot;))</span></p><p class="c3 c1"><span class="c0"></span></p><p class="c2 c1 c11"><span class="c8 c0">Jim Mary Jennifer</span></p><h1 class="c1"><a name="h.eb0bxle2oykl"></a><span>Relational Algebra</span></h1><p class="c9 c1"><span>Remember last century&#39;s SELECT FROM WHERE statements? Well, JSONiq didn&#39;t throw out the baby with the bathwater. It has selection, projection and join capability.</span></p><p class="c3 c1"><span></span></p><p class="c1"><span class="c0">let $stores :=</span></p><p class="c1"><span class="c0">[</span></p><p class="c1"><span class="c0">&nbsp; { &quot;store number&quot; : 1, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c1"><span class="c0">&nbsp; { &quot;store number&quot; : 2, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c1"><span class="c0">&nbsp; { &quot;store number&quot; : 3, &quot;state&quot; : &quot;CA&quot; },</span></p><p class="c1"><span class="c0">&nbsp; { &quot;store number&quot; : 4, &quot;state&quot; : &quot;CA&quot; }</span></p><p class="c1"><span class="c0">]</span></p><p class="c1"><span class="c0">let $sales := [</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;broiler&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 20 &nbsp;},</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 100 },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 50 },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 50 },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 100 },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 150 },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 500 },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 10 },</span></p><p class="c1"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;shirt&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 10 }</span></p><p class="c1"><span class="c0">]</span></p><p class="c1"><span class="c0">let $join :=</span></p><p class="c1"><span class="c0">&nbsp; for $store in jn:members($stores), $sale in jn:members($sales)</span></p><p class="c1"><span class="c0">&nbsp; where $store(&quot;store number&quot;) = $sale(&quot;store number&quot;)</span></p><p class="c1"><span class="c0">&nbsp; return {</span></p><p class="c1"><span class="c0">&nbsp; &nbsp; &quot;nb&quot; : $store(&quot;store number&quot;),</span></p><p class="c1"><span class="c0">&nbsp; &nbsp; &quot;state&quot; : $store(&quot;state&quot;),</span></p><p class="c1"><span class="c0">&nbsp; &nbsp; &quot;sold&quot; : $sale(&quot;product&quot;)</span></p><p class="c1"><span class="c0">&nbsp; }</span></p><p class="c1"><span class="c0">return [$join]</span></p><p class="c15 c1"><span class="c8 c0">[ </span></p><p class="c15 c1"><span class="c8 c0">{ &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;broiler&quot; },</span></p><p class="c15 c1"><span class="c8 c0">{ &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c15 c1"><span class="c8 c0">{ &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c15 c1"><span class="c8 c0">{ &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c15 c1"><span class="c8 c0">{ &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c15 c1"><span class="c8 c0">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c1 c15"><span class="c8 c0">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c15 c1"><span class="c8 c0">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c15 c1"><span class="c8 c0">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;shirt&quot; }</span></p><p class="c15 c1"><span class="c8 c0">&nbsp;]</span></p><h1 class="c1"><a name="h.oii6pvliilhi"></a><span>Access external data</span></h1><p class="c1"><span>Our implementation supports collections of JSON objects or arrays:</span></p><p class="c3 c1"><span></span></p><p class="c1"><span class="c0">dml:collection(&quot;my:data&quot;)</span></p><p class="c2 c1"><span class="c8 c0">{ &quot;foo&quot; : &quot;Your&quot; }</span></p><p class="c2 c1"><span class="c8 c0">{ &quot;foo&quot; : &quot;Collection&quot; }</span></p><p class="c2 c1"><span class="c8 c0">{ &quot;foo&quot; : &quot;of&quot; }</span></p><p class="c2 c1"><span class="c8 c0">{ &quot;foo&quot; : &quot;JSON&quot; }</span></p><p class="c2 c1"><span class="c8 c0">{ &quot;foo&quot; : &quot;objects&quot; }</span></p><p class="c2 c3 c1"><span class="c8 c0"></span></p><p class="c1"><span>It is also possible to get JSON content with an HTTP request, or by parsing it from a string. Functions that handle this are available.</span></p><p class="c3 c1"><span class="c8 c0"></span></p><h1 class="c1"><a name="h.fopcxnkoydgt"></a><span>JSON and XML</span></h1><p class="c9 c1"><span>You can readily use XML and JSON in the same program. If you put a JSON array in an XML constructor, it will be flattened (its member values are recursively taken, even in arrays of arrays). If you put a JSON pair in an XML constructor, it will be unboxed (its value is taken).</span></p><p class="c3 c1"><span></span></p><p class="c1"><span class="c0">let $data := {</span></p><p class="c1"><span class="c0">&nbsp; &quot;color&quot; : &quot;blue&quot;,</span></p><p class="c1"><span class="c0">&nbsp; &quot;closed&quot; : true,</span></p><p class="c1"><span class="c0">&nbsp; &quot;points&quot; : [[10,10], [20,10], [20,20], [10,20]]</span></p><p class="c1"><span class="c0">}</span></p><p class="c1"><span class="c0">let $stroke := attribute stroke { $data(&quot;color&quot;) }</span></p><p class="c1"><span class="c0">let $points := attribute points { jn:flatten($data(&quot;points&quot;)) }</span></p><p class="c1"><span class="c0">return</span></p><p class="c1"><span class="c0">&nbsp; if ($data(&quot;closed&quot;)) then</span></p><p class="c1"><span class="c0">&nbsp; &nbsp; &lt;svg&gt;&lt;polygon&gt;{ $stroke, $points }&lt;/polygon&gt;&lt;/svg&gt;</span></p><p class="c1"><span class="c0">&nbsp; else</span></p><p class="c1"><span class="c0">&nbsp; &nbsp; &lt;svg&gt;&lt;polyline&gt;{ $stroke, $points }&lt;/polyline&gt;&lt;/svg&gt;</span></p><p class="c3 c1"><span class="c0"></span></p><p class="c2 c1 c12"><span class="c8 c0">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></p><p class="c2 c1 c12"><span class="c8 c0">&lt;svg&gt;&lt;polygon stroke=&quot;blue&quot; points=&quot;10 10 20 10 20 20 10 20&quot;/&gt;&lt;/svg&gt;</span></p><p class="c3 c1"><span></span></p><h1 class="c1"><a name="h.bm3buqmzhtko"></a><span>I want more</span></h1><p class="c9 c1"><span>JSONiq supports JSON updates. You can declaratively update your JSON data. JSONiq provides functions that produce a list of updates. The list of updates that is eventually output by your program is then applied to your JSON data.</span></p><p class="c3 c1"><span></span></p><p class="c1"><span class="c0">copy $people := {</span></p><p class="c1"><span class="c0">&nbsp;&quot;John&quot; : { &quot;status&quot; : &quot;single&quot; },</span></p><p class="c1"><span class="c0">&nbsp;&quot;Mary&quot; : { &quot;status&quot; : &quot;single&quot; } }</span></p><p class="c1"><span class="c0">modify (replace json value of $people(&quot;John&quot;)(&quot;status&quot;) with &quot;married&quot;,</span></p><p class="c1"><span class="c0">&nbsp; &nbsp; &nbsp; &nbsp;replace json value of $people(&quot;Mary&quot;)(&quot;status&quot;) with &quot;married&quot;)</span></p><p class="c1"><span class="c0">return $people</span></p><p class="c11 c3 c1"><span class="c0"></span></p><p class="c1 c2"><span class="c8 c0">{ &quot;John&quot; : { &quot;status&quot; : &quot;married&quot; }, &quot;Mary&quot; : { &quot;status&quot; : &quot;married&quot; } }</span></p><p class="c3 c1"><span></span></p><p class="c1"><span>JSONiq works with the XQuery 3.0 standard (switch, typeswitch and try-catch expressions, universal/existential quantifiers, path expressions, filtering expressions, functors, mappings, grouping, windowing will work). The Zorba implementation is also compatible with the proprietary Zorba scripting.</span></p><p class="c3 c1"><span></span></p><p class="c9 c1"><span>The complete JSONiq specification is available on </span><span class="c5"><a class="c13" href="http://www.jsoniq.org/">http://www.jsoniq.org/</a></span></p><p class="c3 c1"><span></span></p></body></html>
8\ No newline at end of file
9
10=== modified file 'doc/zorba/XQueryforJSON.html'
11--- doc/zorba/XQueryforJSON.html 2012-02-09 18:10:20 +0000
12+++ doc/zorba/XQueryforJSON.html 2012-07-25 13:53:23 +0000
13@@ -1,1 +1,1 @@
14-<html><head><title>XQuery for JSON</title><style type="text/css">@import url('https://themes.googleusercontent.com/fonts/css?kit=lhDjYqiy3mZ0x6ROQEUoUw');ol{margin:0;padding:0}p{margin:0}.c21{max-width:468pt;background-color:#ffffff;padding:72pt 72pt 72pt 72pt}.c13{list-style-type:disc;margin:0;padding:0}.c11{color:#1155cc;text-decoration:underline}.c16{text-indent:-13.5pt;margin-left:76.5pt}.c8{color:inherit;text-decoration:inherit}.c19{color:#4a86e8}.c1{font-family:Consolas}.c7{height:18pt}.c20{color:#ff9900}.c10{text-align:justify}.c2{text-align:right}.c6{margin-left:36pt}.c0{direction:ltr}.c3{height:11pt}.c17{color:#ff0000}.c12{font-style:italic}.c18{font-weight:bold}.c5{color:#38761d}.c9{margin-left:18pt}.c15{text-align:center}.c14{padding-left:0pt}.c4{margin-left:144pt}.title{padding-top:24pt;line-height:1.15;text-align:center;color:#4a86e8;font-size:36pt;font-family:Arial;font-weight:bold;padding-bottom:6pt}.subtitle{padding-top:18pt;line-height:1.15;text-align:left;color:#666666;font-style:italic;font-size:24pt;font-family:Georgia;padding-bottom:4pt}body{color:#000000;font-size:11pt;font-family:Arial}h1{padding-top:24pt;line-height:1.15;text-align:left;color:#4a86e8;font-size:24pt;font-family:Arial;font-weight:bold;padding-bottom:6pt}h2{padding-top:18pt;line-height:1.15;text-align:left;color:#ff9900;font-size:18pt;font-family:Arial;font-weight:bold;padding-bottom:4pt}h3{padding-top:14pt;line-height:1.15;text-align:left;color:#000000;font-size:14pt;font-family:Arial;font-weight:bold;padding-bottom:4pt}h4{padding-top:12pt;line-height:1.15;text-align:left;color:#000000;font-size:12pt;font-family:Arial;font-weight:bold;padding-bottom:2pt}h5{padding-top:11pt;line-height:1.15;text-align:left;color:#000000;font-size:11pt;font-family:Arial;font-weight:bold;padding-bottom:2pt}h6{padding-top:10pt;line-height:1.15;text-align:left;color:#000000;font-size:10pt;font-family:Arial;font-weight:bold;padding-bottom:2pt}</style></head><body class="c21"><p class="c0 title"><a name="h.li2wdq8unbq"></a><span>JSONiq for JSON users</span></p><p class="c0 title"><a name="h.5ipqqclfxk4n"></a><span class="c5">XQuery for JSON</span></p><hr><p class="c0 c15"><span>This tutorial introduces the JSONiq language, which declaratively manipulates JSON data.</span></p><p class="c0 c15"><span>Why don&#39;t you go ahead can try the queries of this document on our online demo interface?</span></p><p class="c0 c15"><span>&nbsp;</span><span class="c11 c18"><a class="c8" href="http://jsoniq.zorba-xquery.com/">http://jsoniq.zorba-xquery.com/</a></span></p><hr><p class="c0 c3"><span></span></p><h2 class="c0 c7"><a name="h.8gra0ylxutrv"></a></h2><p class="c0 c9"><span class="c11"><a class="c8" href="#h.f453h84m2u1a">JSON</a></span></p><p class="c0 c9"><span class="c11"><a class="c8" href="#h.qtfakecy3fr7">Elevator Pitch</a></span></p><p class="c0 c9"><span class="c11"><a class="c8" href="#h.yugk1mszt0re">And here you go</a></span></p><p class="c6 c0"><span class="c11"><a class="c8" href="#h.4uvhbej4btrt">Like Monsieur Jourdain, you already knew some JSONiq</a></span></p><p class="c0 c9"><span class="c11"><a class="c8" href="#h.1gal1zhyffu4">JSONiq basics</a></span></p><p class="c6 c0"><span class="c11"><a class="c8" href="#h.ncbxtez2z3rn">The real JSONiq Hello, World!</a></span></p><p class="c6 c0"><span class="c11"><a class="c8" href="#h.ifcjheuaxoqc">Numbers and arithmetic operations</a></span></p><p class="c6 c0"><span class="c11"><a class="c8" href="#h.jmbc6c1fqqwx">Logical operations</a></span></p><p class="c6 c0"><span class="c11"><a class="c8" href="#h.iq1ixg5ywx0r">Strings</a></span></p><p class="c6 c0"><span class="c11"><a class="c8" href="#h.rv6w8p4wenfa">Sequences</a></span></p><p class="c0 c9"><span class="c11"><a class="c8" href="#h.7x2r8sj1zjwe">A bit more in depth</a></span></p><p class="c6 c0"><span class="c11"><a class="c8" href="#h.b6c9zq9um8cu">Variables</a></span></p><p class="c6 c0"><span class="c11"><a class="c8" href="#h.92ak2udyfghp">Iteration</a></span></p><p class="c6 c0"><span class="c11"><a class="c8" href="#h.zdhx60a5duyu">Conditions</a></span></p><p class="c6 c0"><span class="c11"><a class="c8" href="#h.ccs6lkxiixw4">Composability of Expressions</a></span></p><p class="c0 c9"><span class="c11"><a class="c8" href="#h.mjv7kyjurri2">JSON Navigation</a></span></p><p class="c6 c0"><span class="c11"><a class="c8" href="#h.xosiyb17dh24">Objects</a></span></p><p class="c6 c0"><span class="c11"><a class="c8" href="#h.mrf42gizl3y6">Arrays</a></span></p><p class="c6 c0"><span class="c11"><a class="c8" href="#h.hdc3dgohtdut">Pairs</a></span></p><p class="c0 c9"><span class="c11"><a class="c8" href="#h.eb0bxle2oykl">Relational Algebra</a></span></p><p class="c0 c9"><span class="c11"><a class="c8" href="#h.oii6pvliilhi">Access external data</a></span></p><p class="c0 c9"><span class="c11"><a class="c8" href="#h.bm3buqmzhtko">I want more</a></span></p><p class="c6 c0"><span class="c11"><a class="c8" href="#h.5itcphr8262b">Even more</a></span></p><p class="c0 c6"><span class="c11"><a class="c8" href="#h.j7oh3cyhem38">More is not enough</a></span></p><p><span></span></p><p class="c0 c3"><span></span></p><h1 class="c0"><a name="h.f453h84m2u1a"></a><span>JSON</span></h1><p class="c10 c0"><span>As explained on </span><span class="c11"><a class="c8" href="http://www.json.org/">http://www.json.org/</a></span><span>, JSON is a lightweight data-interchange format designed for humans as well as for computers. It supports as values:</span></p><ol class="c13" start="1"><li class="c10 c6 c0 c14"><span>objects (string-to-value map)</span></li><li class="c10 c6 c0 c14"><span>arrays (ordered sequence of values)</span></li><li class="c10 c6 c0 c14"><span>strings</span></li><li class="c10 c6 c0 c14"><span>numbers</span></li><li class="c10 c6 c0 c14"><span>booleans (true, false)</span></li><li class="c10 c6 c0 c14"><span>null</span></li></ol><p class="c0 c3"><span></span></p><p class="c0"><span>JSONiq provides declarative querying and updating capabilities on JSON data.</span></p><h1 class="c0"><a name="h.qtfakecy3fr7"></a><span>Elevator Pitch</span></h1><p class="c10 c0"><span>JSONiq is based on XQuery, which is a W3C standard (like XML and HTML). XQuery is a very powerful declarative language that manipulates XML data, but that is also a very good fit for JSON. Here is an appetizer before we start the tutorial from scratch.</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">let $stores :=</span></p><p class="c0"><span class="c1">[</span></p><p class="c0"><span class="c1">&nbsp; { &quot;store number&quot; : 1, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c0"><span class="c1">&nbsp; { &quot;store number&quot; : 2, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c0"><span class="c1">&nbsp; { &quot;store number&quot; : 3, &quot;state&quot; : &quot;CA&quot; },</span></p><p class="c0"><span class="c1">&nbsp; { &quot;store number&quot; : 4, &quot;state&quot; : &quot;CA&quot; }</span></p><p class="c0"><span class="c1">]</span></p><p class="c0"><span class="c1">let $sales := [</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;broiler&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 20 &nbsp;},</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 100 },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 50 },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 50 },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 100 },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 150 },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 500 },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 10 },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;shirt&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 10 }</span></p><p class="c0"><span class="c1">]</span></p><p class="c0"><span class="c1">let $join :=</span></p><p class="c0"><span class="c1">&nbsp; for $store in jn:values($stores), $sale in jn:values($sales)</span></p><p class="c0"><span class="c1">&nbsp; where $store(&quot;store number&quot;) = $sale(&quot;store number&quot;)</span></p><p class="c0"><span class="c1">&nbsp; return {</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; &quot;nb&quot; : $store(&quot;store number&quot;),</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; &quot;state&quot; : $store(&quot;state&quot;),</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; &quot;sold&quot; : $sale(&quot;product&quot;)</span></p><p class="c0"><span class="c1">&nbsp; }</span></p><p class="c0"><span class="c1">return [$join]</span></p><p class="c0 c4"><span class="c1 c5">[ </span></p><p class="c0 c4"><span class="c1 c5">&nbsp; { &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;broiler&quot; },</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; { &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; { &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; { &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; { &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;shirt&quot; }</span></p><p class="c0 c4"><span class="c1 c5">&nbsp;]</span></p><p class="c0 c3"><span></span></p><h1 class="c0"><a name="h.yugk1mszt0re"></a><span>And here you go</span></h1><h2 class="c0"><a name="h.4uvhbej4btrt"></a><span>Like Monsieur Jourdain, you already knew some JSONiq</span></h2><p class="c10 c0"><span>The first thing you need to know is that every well-formed JSON document is a JSONiq query as well.</span></p><p class="c0 c3"><span></span></p><p class="c10 c0"><span>This means that you can copy-and-paste a JSON document into a query. The following are JSONiq queries that are &quot;idempotent&quot; (they just output themselves):</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">{ &quot;pi&quot; : 3.14, &quot;sq2&quot; : 1.4 }</span></p><p class="c2 c0"><span class="c1 c5">{ &quot;pi&quot; : 3.14, &quot;sq2&quot; : 1.4 }</span></p><p class="c0 c3"><span class="c1 c5"></span></p><p class="c0"><span class="c1">[ 2, 3, 5, 7, 11, 13 ]</span></p><p class="c2 c0"><span class="c1 c5">[ 2, 3, 5, 7, 11, 13 ]</span></p><p class="c0 c3"><span class="c1"></span></p><p class="c0"><span class="c1">{</span></p><p class="c0"><span class="c1">&nbsp; &quot;operations&quot; : [</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; { &quot;binary&quot; : [ &quot;and&quot;, &quot;or&quot;] },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; { &quot;unary&quot; : [&quot;not&quot;] }</span></p><p class="c0"><span class="c1">&nbsp; ],</span></p><p class="c0"><span class="c1">&nbsp; &quot;bits&quot; : [</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; 0, 1</span></p><p class="c0"><span class="c1">&nbsp; ]</span></p><p class="c0"><span class="c1">}</span></p><p class="c0 c4"><span class="c1 c5">{</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; &quot;operations&quot; : [</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; &nbsp; { &quot;binary&quot; : [ &quot;and&quot;, &quot;or&quot; ] },</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; &nbsp; { &quot;unary&quot; : [ &quot;not&quot; ] }</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; ],</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; &quot;bits&quot; : [</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; &nbsp; 0, 1</span></p><p class="c0 c4"><span class="c1 c5">&nbsp; ]</span></p><p class="c0 c4"><span class="c1 c5">}</span></p><p class="c2 c0 c3"><span class="c1 c5"></span></p><p class="c0"><span>This works with objects, arrays (even nested), strings, numbers, booleans, null. </span><span class="c12">The exceptions to this rule (but we are working on it!) are that:</span></p><p class="c10 c0"><span class="c12">(i) if a pair has a true, false or null value, there must be a space on at least one side of the colon (this will be fixed).</span></p><p class="c10 c0"><span class="c12">(ii) empty objects are not recognized</span></p><p class="c10 c0"><span class="c12">(iii) characters escaped with the \ in JSON strings are not recognized</span></p><p class="c0 c3"><span class="c1 c17 c12"></span></p><p class="c10 c0"><span>It also works the other way round: if your query outputs an object or an array, you can readily use it as a JSON document.</span></p><p class="c0 c3"><span></span></p><p class="c10 c0"><span>JSONiq is a declarative language. This means that you only need to say </span><span class="c12">what</span><span>&nbsp;you want - the compiler will take care of the </span><span class="c12">how</span><span>. In the above queries, you are basically saying: I want to output this JSON content, and here it is.</span></p><h1 class="c0"><a name="h.1gal1zhyffu4"></a><span>JSONiq basics</span></h1><h2 class="c0"><a name="h.ncbxtez2z3rn"></a><span>The real JSONiq Hello, World!</span></h2><p class="c0"><span>Wondering what a hello world program looks like in JSONiq? Here it is:</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">&quot;Hello, World!&quot;</span></p><p class="c2 c0"><span class="c1 c5">Hello, World!</span></p><p class="c0 c3"><span class="c1 c5"></span></p><p class="c0"><span>Not surprisingly, it outputs the string &quot;Hello, World!&quot;. Again, just say what you want and you will get it.</span></p><h2 class="c0"><a name="h.ifcjheuaxoqc"></a><span>Numbers and arithmetic operations</span></h2><p class="c10 c0"><span>Okay, so, now, you might be thinking: &quot;What is the use of this language if it just outputs what I put in?&quot; Of course, JSONiq can more than that. And still in a declarative way. Here is how it works with numbers:</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">2 + 2</span></p><p class="c0 c3"><span class="c1"></span></p><p class="c0"><span>will surprisingly output:</span></p><p class="c2 c0"><span class="c1 c5">4</span></p><p class="c0 c3"><span></span></p><p class="c0"><span>whereas</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">(38 + 2) div 2 + 11 * 2</span></p><p class="c0 c3"><span class="c1"></span></p><p class="c0"><span>will output</span></p><p class="c2 c0"><span class="c1 c5">42</span></p><p class="c0 c3"><span class="c1 c5"></span></p><p class="c10 c0"><span>(mind the division operator which is the &quot;div&quot; keyword. The slash has other semantics).</span></p><p class="c0 c3"><span></span></p><p class="c0"><span>Like JSON, JSONiq works with decimals, too:</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">6.022e23 * 42</span></p><p class="c2 c0"><span class="c1 c5">2.52924E25</span></p><h2 class="c0"><a name="h.jmbc6c1fqqwx"></a><span>Logical operations</span></h2><p class="c10 c0"><span>JSONiq supports boolean operations.</span></p><p class="c10 c0 c3"><span></span></p><p class="c0"><span class="c1">true and false</span></p><p class="c2 c0"><span class="c1 c5">false</span></p><p class="c2 c0 c3"><span class="c1 c5"></span></p><p class="c0"><span class="c1">(true or false) and (false or true)</span></p><p class="c2 c0"><span class="c1 c5">true</span></p><p class="c2 c0 c3"><span class="c1 c5"></span></p><p class="c0"><span>The unary not is a function (yes, JSONiq has functions, and they are in the same way as in many languages):</span></p><p class="c0 c3"><span class="c1"></span></p><p class="c0"><span class="c1">not(true)</span></p><p class="c0 c2"><span class="c1 c5">false</span></p><h2 class="c0"><a name="h.iq1ixg5ywx0r"></a><span>Strings</span></h2><p class="c0"><span>JSONiq is capable of manipulating strings as well, using functions:</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">concat(&quot;Hello &quot;, &quot;Captain Kirk&quot;)</span></p><p class="c2 c0"><span class="c1 c5">Hello Captain Kirk</span></p><p class="c2 c0 c3"><span class="c1 c5"></span></p><p class="c0"><span class="c1">substring(&quot;Mister Spock&quot;, 8, 5)</span></p><p class="c2 c0"><span class="c1 c5">Spock</span></p><p class="c2 c0 c3"><span class="c5"></span></p><p class="c10 c0"><span>JSONiq comes up with a rich string function library out of the box inherited from its base language. These functions are listed </span><span class="c11"><a class="c8" href="http://www.w3.org/TR/xpath-functions-30/#string-functions">here</a></span><span>&nbsp;(actually, you will find even many more for numbers, etc.)</span></p><h2 class="c0"><a name="h.rv6w8p4wenfa"></a><span>Sequences</span></h2><p class="c0"><span>Until now, we only handled single values (an object, an array, a number, a string, a boolean). JSONiq supports sequences of values. You can build a sequence using commas:</span></p><p class="c0 c3"><span></span></p><p class="c0"><span>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</span></p><p class="c2 c0"><span class="c5">1 2 3 4 5 6 7 8 9 10</span></p><p class="c0 c3"><span class="c5"></span></p><p class="c0"><span class="c1">1, </span><span class="c1">true</span><span class="c1">, 4.2e1, &quot;Life&quot;</span></p><p class="c2 c0"><span class="c1 c5">1 true 42 Life</span></p><p class="c0 c3"><span class="c1"></span></p><p class="c0"><span class="c1">{ &quot;Question&quot; : &quot;Ultimate&quot; }, [&quot;Life&quot;, &quot;The universe&quot;, &quot;and everything&quot;]</span></p><p class="c2 c0 c3"><span class="c1"></span></p><p class="c2 c0"><span class="c1 c5">{ &quot;Question&quot; : &quot;Ultimate&quot; }[ &quot;Life&quot;, &quot;The universe&quot;, &quot;and everything&quot; ]</span></p><p class="c2 c0 c3"><span class="c1"></span></p><p class="c0"><span>The &quot;to&quot; operator is very convenient, too:</span></p><p class="c0 c3"><span class="c1"></span></p><p class="c0"><span class="c1">1 to 100</span></p><p class="c2 c0"><span class="c5">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20</span></p><p class="c2 c0"><span class="c5">21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40</span></p><p class="c2 c0"><span class="c5">41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60</span></p><p class="c2 c0"><span class="c5">61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80</span></p><p class="c2 c0"><span class="c5">81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100</span></p><p class="c0 c3"><span></span></p><p class="c0"><span>Some functions even work on sequences:</span></p><p class="c0 c3"><span class="c1"></span></p><p class="c0"><span class="c1">sum(1 to 100)</span></p><p class="c2 c0"><span class="c1 c5">5050</span></p><p class="c2 c0 c3"><span class="c1 c5"></span></p><p class="c0"><span class="c1">string-join((&quot;These&quot;, &quot;are&quot;, &quot;some&quot;, &quot;words&quot;), &quot;-&quot;)</span></p><p class="c2 c0"><span class="c1 c5">These-are-some-words</span></p><p class="c2 c0 c3"><span class="c1 c5"></span></p><p class="c0"><span class="c1">count(10 to 20)</span></p><p class="c2 c0"><span class="c1 c5">11</span></p><p class="c0 c3"><span class="c1 c5"></span></p><p class="c0"><span class="c1">avg(1 to 100)</span></p><p class="c2 c0"><span class="c1 c5">50.5</span></p><h1 class="c0"><a name="h.7x2r8sj1zjwe"></a><span>A bit more in depth</span></h1><h2 class="c0"><a name="h.b6c9zq9um8cu"></a><span>Variables</span></h2><p class="c0"><span>You can bind a sequence of values to a (dollar-prefixed) variable, like so:</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">let $x := &quot;Bearing 3 1 4 Mark 5. &quot;</span></p><p class="c0"><span class="c1">return concat($x, &quot;Engage!&quot;)</span></p><p class="c2 c0"><span class="c1 c5">Bearing 3 1 4 Mark 5. Engage!</span></p><p class="c2 c0 c3"><span class="c1 c5"></span></p><p class="c2 c0 c3"><span class="c1"></span></p><p class="c0"><span class="c1">let $x := (&quot;Kirk&quot;, &quot;Picard&quot;, &quot;Sisko&quot;)</span></p><p class="c0"><span class="c1">return string-join($x, &quot; and &quot;)</span></p><p class="c2 c0"><span class="c1 c5">Kirk and Picard and Sisko</span></p><p class="c0 c3"><span class="c1 c5"></span></p><p class="c0"><span>You can bind as many variables as you want:</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">let $x := 1</span></p><p class="c0"><span class="c1">let $y := $x * 2</span></p><p class="c0"><span class="c1">let $z := $y + $x</span></p><p class="c0"><span class="c1">return [$x, $y, $z]</span></p><p class="c2 c0"><span class="c1 c5">[ 1, 2, 3 ]</span></p><p class="c2 c0 c3"><span class="c1 c5"></span></p><p class="c0"><span>and even reuse the same name:</span></p><p class="c0"><span class="c1">let $x := 1</span></p><p class="c0"><span class="c1">let $x := $x + 2</span></p><p class="c0"><span class="c1">let $x := $x + 3</span></p><p class="c0"><span class="c1">return $x</span></p><p class="c2 c0"><span class="c1 c5">6</span></p><h2 class="c0"><a name="h.92ak2udyfghp"></a><span>Iteration</span></h2><p class="c10 c0"><span>In a way very similar to let, you can iterate over a sequence of values with the &quot;for&quot; keyword. Instead of binding the entire sequence of the variable, it will bind each value of the sequence in turn to this variable.</span></p><p class="c10 c0 c3"><span></span></p><p class="c10 c0"><span class="c1">for $i in 1 to 10</span></p><p class="c10 c0"><span class="c1">return $i * 2</span></p><p class="c2 c0"><span class="c1 c5">2 4 6 8 10 12 14 16 18 20</span></p><p class="c2 c0 c3"><span class="c1 c5"></span></p><p class="c0"><span>More interestingly, you can combine fors and lets like so:</span></p><p class="c0 c3"><span class="c1"></span></p><p class="c0"><span class="c1">let $sequence := 1 to 10</span></p><p class="c0"><span class="c1">for $value in $sequence</span></p><p class="c0"><span class="c1">let $square := $value * 2</span></p><p class="c0"><span class="c1">return $square</span></p><p class="c2 c0"><span class="c1 c5">2 4 6 8 10 12 14 16 18 20</span></p><p class="c2 c0 c3"><span class="c1 c5"></span></p><p class="c0"><span>and even filter out some values:</span></p><p class="c0 c3"><span class="c1"></span></p><p class="c0"><span class="c1">let $sequence := 1 to 10</span></p><p class="c0"><span class="c1">for $value in $sequence</span></p><p class="c0"><span class="c1">let $square := $value * 2</span></p><p class="c0"><span class="c1">where $square &lt; 10</span></p><p class="c0"><span class="c1">return $square</span></p><p class="c2 c0"><span class="c1 c5">2 4 6 8</span></p><h2 class="c0"><a name="h.zdhx60a5duyu"></a><span>Conditions</span></h2><p class="c0"><span>You can make the output depend on a condition with an if-then-else construct:</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">for $x in 1 to 10</span></p><p class="c0"><span class="c1">return if ($x &lt; 5) then $x</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else -$x</span></p><p class="c2 c0"><span class="c1 c5">1 2 3 4 -5 -6 -7 -8 -9 -10</span></p><h2 class="c0"><a name="h.ccs6lkxiixw4"></a><span>Composability of Expressions</span></h2><p class="c0"><span>Now that you know of a couple of elementary JSONiq expressions, you can combine then in more elaborate expressions. For example, you can put any sequence of values in an array:</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">[ 1 to 10 ]</span></p><p class="c2 c0"><span class="c1 c5">[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]</span></p><p class="c2 c0 c3"><span class="c1 c5"></span></p><p class="c0"><span>Or you can dynamically compute the value of object pairs:</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">{</span></p><p class="c0"><span class="c1">&nbsp; &quot;Greeting&quot; : (let $d := &quot;Mister Spock&quot;</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return concat(&quot;Hello, &quot;, $d)),</span></p><p class="c0"><span class="c1">&nbsp; &quot;Farewell&quot; : string-join((&quot;Live&quot;, &quot;long&quot;, &quot;and&quot;, &quot;prosper&quot;),</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot; &quot;)</span></p><p class="c0"><span class="c1">}</span></p><p class="c0 c3"><span class="c1 c5"></span></p><p class="c2 c0"><span class="c1 c5">{ &quot;Greeting&quot; : &quot;Hello, Mister Spock&quot;, &quot;Farewell&quot; : &quot;Live long and prosper&quot; }</span></p><p class="c0 c3"><span class="c5"></span></p><p class="c0"><span>You can also dynamically generate pairs:</span></p><p class="c0 c3"><span class="c1"></span></p><p class="c0"><span class="c1">concat(&quot;Integer &quot;, 2) : 2 * 2</span></p><p class="c2 c0"><span class="c1 c5">&quot;Integer 2&quot; : 4</span></p><p class="c2 c0 c3"><span class="c1 c5"></span></p><p class="c0"><span>and then wrap lots of them in an object:</span></p><p class="c0 c3"><span class="c1"></span></p><p class="c0"><span class="c1">{</span></p><p class="c0"><span class="c1">&nbsp; for $i in 1 to 10</span></p><p class="c0"><span class="c1">&nbsp; return concat(&quot;Square of &quot;, $i) : $i * $i</span></p><p class="c0"><span class="c1">}</span></p><p class="c0 c4"><span class="c1 c5">{</span></p><p class="c0 c4"><span class="c1 c5">&quot;Square of 1&quot; : 1,</span></p><p class="c0 c4"><span class="c1 c5">&quot;Square of 2&quot; : 4,</span></p><p class="c0 c4"><span class="c1 c5">&quot;Square of 3&quot; : 9,</span></p><p class="c0 c4"><span class="c1 c5">&quot;Square of 4&quot; : 16,</span></p><p class="c0 c4"><span class="c1 c5">&quot;Square of 5&quot; : 25,</span></p><p class="c0 c4"><span class="c1 c5">&quot;Square of 6&quot; : 36,</span></p><p class="c0 c4"><span class="c1 c5">&quot;Square of 7&quot; : 49,</span></p><p class="c0 c4"><span class="c1 c5">&quot;Square of 8&quot; : 64,</span></p><p class="c0 c4"><span class="c1 c5">&quot;Square of 9&quot; : 81,</span></p><p class="c0 c4"><span class="c1 c5">&quot;Square of 10&quot; : 100</span></p><p class="c0 c4"><span class="c1 c5">}</span></p><h1 class="c0"><a name="h.mjv7kyjurri2"></a><span>JSON Navigation</span></h1><p class="c10 c0"><span>Up to now, you learnt how to compose expressions so as to do some computations and to build pairs, objects and arrays. It also works the other way round: if you have some JSON data, you can access it and navigate.</span></p><p class="c10 c0 c3"><span></span></p><p class="c0 c10"><span>All you need to know is: JSONiq views</span></p><ol class="c13" start="1"><li class="c10 c6 c0 c14"><span>an array as a sequence of values,</span></li><li class="c10 c6 c0 c14"><span>an object as a set of name/value pairs (we call them just: pairs)</span></li><li class="c10 c6 c0 c14"><span>a pair as a (name, value) tuple.</span></li></ol><h2 class="c10 c0"><a name="h.xosiyb17dh24"></a><span>Objects</span></h2><p class="c10 c0"><span>If you use an object as a functor and provide it with a string, it will return the pair named after its parameter:</span></p><p class="c0 c3"><span class="c1"></span></p><p class="c0"><span class="c1">let $person := {</span></p><p class="c0"><span class="c1">&nbsp; &quot;first name&quot; : &quot;Sarah&quot;,</span></p><p class="c0"><span class="c1">&nbsp; &quot;age&quot; : 13,</span></p><p class="c0"><span class="c1">&nbsp; &quot;gender&quot; : &quot;female&quot;,</span></p><p class="c0"><span class="c1">&nbsp; &quot;friends&quot; : [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c0"><span class="c1">}</span></p><p class="c0"><span class="c1">return $person(&quot;first name&quot;)</span></p><p class="c2 c0"><span class="c1 c5">&quot;first name&quot; : &quot;Sarah&quot;</span></p><p class="c2 c0 c3"><span class="c1 c5"></span></p><h2 class="c0"><a name="h.mrf42gizl3y6"></a><span>Arrays</span></h2><p class="c10 c0"><span>If you use an array as a functor and provide it with an integer, it will return the corresponding entry:</span></p><p class="c0 c3"><span class="c1"></span></p><p class="c0"><span class="c1">let $friends := [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c0"><span class="c1">return $friends(2)</span></p><p class="c2 c0"><span class="c1 c5">Mary</span></p><h2 class="c0"><a name="h.hdc3dgohtdut"></a><span>Pairs</span></h2><p class="c0"><span>Given a pair, you can get its name and value using the functions jn:name and jn:value:</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">let $pair := &quot;name of the pair&quot; : &quot;value of the pair&quot;</span></p><p class="c0"><span class="c1">return { &nbsp;&quot;name&quot; : jn:name($pair), &quot;value&quot; : jn:value($pair) }</span></p><p class="c0 c3"><span class="c1"></span></p><p class="c2 c0"><span class="c1 c5">{ &quot;name&quot; : &quot;name of the pair&quot;, &quot;value&quot; : &quot;value of the pair&quot; }</span></p><p class="c0 c3"><span class="c5"></span></p><p class="c0"><span>This functions also exist in plural form for objects:</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">let $person := {</span></p><p class="c0"><span class="c1">&nbsp; &quot;name&quot; : &quot;Sarah&quot;,</span></p><p class="c0"><span class="c1">&nbsp; &quot;age&quot; : 13,</span></p><p class="c0"><span class="c1">&nbsp; &quot;gender&quot; : &quot;female&quot;,</span></p><p class="c0"><span class="c1">&nbsp; &quot;friends&quot; : [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c0"><span class="c1">}</span></p><p class="c0"><span class="c1">return { &quot;names&quot; : [ jn:names($person)],</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;values&quot; : [ jn:values($person)]</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; &nbsp; &nbsp;}</span></p><p class="c0 c16"><span class="c1 c5">{</span></p><p class="c16 c0"><span class="c1 c5">&quot;names&quot; : [ &quot;name&quot;, &quot;age&quot;, &quot;gender&quot;, &quot;friends&quot; ],</span></p><p class="c16 c0"><span class="c1 c5">&quot;values&quot; : [ &quot;Sarah&quot;, 13, &quot;female&quot;, [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot; ] ]</span></p><p class="c16 c0"><span class="c1 c5">}</span></p><p class="c0"><span>jn:values can also be used for an array:</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">let $person := {</span></p><p class="c0"><span class="c1">&nbsp; &quot;name&quot; : &quot;Sarah&quot;,</span></p><p class="c0"><span class="c1">&nbsp; &quot;age&quot; : 13,</span></p><p class="c0"><span class="c1">&nbsp; &quot;gender&quot; : &quot;female&quot;,</span></p><p class="c0"><span class="c1">&nbsp; &quot;friends&quot; : [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c0"><span class="c1">}</span></p><p class="c0"><span class="c1">return jn:values($person(&quot;friends&quot;))</span></p><p class="c2 c0"><span class="c5">[ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot; ]</span></p><p class="c2 c0 c3"><span class="c5"></span></p><h1 class="c0"><a name="h.eb0bxle2oykl"></a><span>Relational Algebra</span></h1><p class="c10 c0"><span>Remember last century&#39;s SELECT FROM WHERE statements? Well, JSONiq didn&#39;t throw out the baby with the bathwater. It has selection, projection and join capability.</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">let $stores :=</span></p><p class="c0"><span class="c1">[</span></p><p class="c0"><span class="c1">&nbsp; { &quot;store number&quot; : 1, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c0"><span class="c1">&nbsp; { &quot;store number&quot; : 2, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c0"><span class="c1">&nbsp; { &quot;store number&quot; : 3, &quot;state&quot; : &quot;CA&quot; },</span></p><p class="c0"><span class="c1">&nbsp; { &quot;store number&quot; : 4, &quot;state&quot; : &quot;CA&quot; }</span></p><p class="c0"><span class="c1">]</span></p><p class="c0"><span class="c1">let $sales := [</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;broiler&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 20 &nbsp;},</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 100 },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 50 },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 50 },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 100 },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 150 },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 500 },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 10 },</span></p><p class="c0"><span class="c1">&nbsp; &nbsp;{ &quot;product&quot; : &quot;shirt&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 10 }</span></p><p class="c0"><span class="c1">]</span></p><p class="c0"><span class="c1">let $join :=</span></p><p class="c0"><span class="c1">&nbsp; for $store in jn:values($stores), $sale in jn:values($sales)</span></p><p class="c0"><span class="c1">&nbsp; where $store(&quot;store number&quot;) = $sale(&quot;store number&quot;)</span></p><p class="c0"><span class="c1">&nbsp; return {</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; &quot;nb&quot; : $store(&quot;store number&quot;),</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; &quot;state&quot; : $store(&quot;state&quot;),</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; &quot;sold&quot; : $sale(&quot;product&quot;)</span></p><p class="c0"><span class="c1">&nbsp; }</span></p><p class="c0"><span class="c1">return [$join]</span></p><p class="c0 c4"><span class="c1 c5">[ </span></p><p class="c0 c4"><span class="c1 c5">{ &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;broiler&quot; },</span></p><p class="c0 c4"><span class="c1 c5">{ &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c0 c4"><span class="c1 c5">{ &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c0 c4"><span class="c1 c5">{ &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c0 c4"><span class="c1 c5">{ &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c0 c4"><span class="c1 c5">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c0 c4"><span class="c1 c5">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c0 c4"><span class="c1 c5">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c0 c4"><span class="c1 c5">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;shirt&quot; }</span></p><p class="c0 c4"><span class="c1 c5">&nbsp;]</span></p><h1 class="c0"><a name="h.oii6pvliilhi"></a><span>Access external data</span></h1><p class="c0"><span>Our implementation even supports collections of JSON objects or arrays:</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">dml:collection(&quot;my:data&quot;)</span></p><p class="c2 c0"><span class="c1 c5">{ &quot;foo&quot; : &quot;Your&quot; }</span></p><p class="c2 c0"><span class="c1 c5">{ &quot;foo&quot; : &quot;Collection&quot; }</span></p><p class="c2 c0"><span class="c1 c5">{ &quot;foo&quot; : &quot;of&quot; }</span></p><p class="c2 c0"><span class="c1 c5">{ &quot;foo&quot; : &quot;JSON&quot; }</span></p><p class="c2 c0"><span class="c1 c5">{ &quot;foo&quot; : &quot;objects&quot; }</span></p><p class="c2 c0 c3"><span class="c1 c5"></span></p><p class="c0"><span>It is also possible to get JSON content with an HTTP request, or by parsing it from a string. Functions that handle this are available.</span></p><h1 class="c0"><a name="h.bm3buqmzhtko"></a><span>I want more</span></h1><p class="c10 c0"><span>JSONiq supports JSON updates. You can declaratively update your JSON data. JSONiq provides functions that produce a list of updates. The list of updates that is eventually output by your program is then applied to your JSON data.</span></p><p class="c0 c3"><span></span></p><p class="c0"><span class="c1">let $my-data := jn:json(&quot;persons.json&quot;)</span></p><p class="c0"><span class="c1">let $john := jn:value($my-data(&quot;John&quot;))</span></p><p class="c0"><span class="c1">let $mary := jn:value($my-data(&quot;Mary&quot;))</span></p><p class="c0"><span class="c1">return (jn:replace-value($john, &quot;status&quot;, &quot;married&quot;),</span></p><p class="c0"><span class="c1">&nbsp; &nbsp; &nbsp; &nbsp; jn:replace-value($mary, &quot;status&quot;, &quot;married&quot;))</span></p><h2 class="c0"><a name="h.5itcphr8262b"></a><span>Even more</span></h2><p class="c0"><span>JSONiq can do way more that what is presented here. Here are a couple of highlights:</span></p><p class="c10 c0"><span>- JSONiq is a strongly typed language, but is smart enough to not bother you with types when unnecessary. It potentially supports static typing as well to detect errors before you even execute your program.</span></p><p class="c0"><span>- You can define your own functions and modules.</span></p><p class="c0"><span>- JSONiq provides you with loads of available modules.</span></p><p class="c10 c0"><span>- JSONiq has tons of further features such as switch, typeswitch and try-catch expressions, universal/existential quantifiers, path expressions, filtering expressions, functors, mappings, grouping, windowing.</span></p><h2 class="c0"><a name="h.j7oh3cyhem38"></a><span>More is not enough</span></h2><p class="c10 c0"><span>- JSONiq supports XML. Yes: you can manipulate JSON and XML with the same language! JSONiq is actually a superset of XQuery, a W3C standard, and extends its data model to support JSON.</span></p><p class="c10 c0"><span>- JSONiq supports scripting. If you need to write a full-fledged, side-effecting Web application, scripting is for you.</span></p></body></html>
15\ No newline at end of file
16+<html><head><title>XQuery for JSON</title><style type="text/css">@import url('https://themes.googleusercontent.com/fonts/css?kit=lhDjYqiy3mZ0x6ROQEUoUw');ol{margin:0;padding:0}.c19{list-style-type:disc;margin:0;padding:0}.c21{max-width:468pt;background-color:#ffffff;padding:72pt 72pt 72pt 72pt}.c16{text-indent:-13.5pt;margin-left:76.5pt}.c4{height:11pt;text-align:right}.c7{padding-left:0pt;text-align:justify}.c1{color:#1155cc;text-decoration:underline}.c14{color:inherit;text-decoration:inherit}.c3{direction:ltr}.c20{height:18pt}.c8{margin-left:144pt}.c22{color:#ff9900}.c9{text-align:right}.c15{color:#4a86e8}.c13{font-style:italic}.c17{font-weight:bold}.c6{margin-left:36pt}.c12{margin-left:18pt}.c18{color:#ff0000}.c0{font-family:"Consolas"}.c10{text-align:justify}.c2{color:#38761d}.c5{height:11pt}.c11{text-align:center}.title{padding-top:24pt;line-height:1.15;text-align:center;color:#4a86e8;font-size:36pt;font-family:"Arial";font-weight:bold;padding-bottom:6pt}.subtitle{padding-top:18pt;line-height:1.15;text-align:left;color:#666666;font-style:italic;font-size:24pt;font-family:"Georgia";padding-bottom:4pt}li{color:#000000;font-size:11pt;font-family:"Arial"}p{color:#000000;font-size:11pt;margin:0;font-family:"Arial"}h1{padding-top:24pt;line-height:1.15;text-align:left;color:#4a86e8;font-size:24pt;font-family:"Arial";font-weight:bold;padding-bottom:6pt}h2{padding-top:18pt;line-height:1.15;text-align:left;color:#ff9900;font-size:18pt;font-family:"Arial";font-weight:bold;padding-bottom:4pt}h3{padding-top:14pt;line-height:1.15;text-align:left;color:#000000;font-size:14pt;font-family:"Arial";font-weight:bold;padding-bottom:4pt}h4{padding-top:12pt;line-height:1.15;text-align:left;color:#000000;font-size:12pt;font-family:"Arial";font-weight:bold;padding-bottom:2pt}h5{padding-top:11pt;line-height:1.15;text-align:left;color:#000000;font-size:11pt;font-family:"Arial";font-weight:bold;padding-bottom:2pt}h6{padding-top:10pt;line-height:1.15;text-align:left;color:#000000;font-size:10pt;font-family:"Arial";font-weight:bold;padding-bottom:2pt}</style></head><body class="c21"><p class="c3 title"><a name="h.li2wdq8unbq"></a><span>JSONiq for JSON users</span></p><p class="c3 title"><a name="h.5ipqqclfxk4n"></a><span class="c2">XQuery for JSON</span></p><hr><p class="c3 c11"><span>This tutorial introduces the JSONiq language, which declaratively manipulates JSON data.</span></p><p class="c3 c11"><span>Why don&#39;t you go ahead can try the queries of this document on our online demo interface?</span></p><p class="c3 c11"><span>&nbsp;</span><span class="c1 c17"><a class="c14" href="http://jsoniq.zorba-xquery.com/">http://jsoniq.zorba-xquery.com/</a></span></p><hr><p class="c3 c5"><span></span></p><h2 class="c3 c20"><a name="h.8gra0ylxutrv"></a></h2><p class="c3 c12"><span class="c1"><a class="c14" href="#h.f453h84m2u1a">JSON</a></span></p><p class="c3 c12"><span class="c1"><a class="c14" href="#h.qtfakecy3fr7">Elevator Pitch</a></span></p><p class="c3 c12"><span class="c1"><a class="c14" href="#h.yugk1mszt0re">And here you go</a></span></p><p class="c3 c6"><span class="c1"><a class="c14" href="#h.4uvhbej4btrt">Like Monsieur Jourdain, you already knew some JSONiq</a></span></p><p class="c3 c12"><span class="c1"><a class="c14" href="#h.1gal1zhyffu4">JSONiq basics</a></span></p><p class="c3 c6"><span class="c1"><a class="c14" href="#h.ncbxtez2z3rn">The real JSONiq Hello, World!</a></span></p><p class="c3 c6"><span class="c1"><a class="c14" href="#h.ifcjheuaxoqc">Numbers and arithmetic operations</a></span></p><p class="c3 c6"><span class="c1"><a class="c14" href="#h.jmbc6c1fqqwx">Logical operations</a></span></p><p class="c3 c6"><span class="c1"><a class="c14" href="#h.iq1ixg5ywx0r">Strings</a></span></p><p class="c3 c6"><span class="c1"><a class="c14" href="#h.rv6w8p4wenfa">Sequences</a></span></p><p class="c3 c12"><span class="c1"><a class="c14" href="#h.7x2r8sj1zjwe">A bit more in depth</a></span></p><p class="c3 c6"><span class="c1"><a class="c14" href="#h.b6c9zq9um8cu">Variables</a></span></p><p class="c3 c6"><span class="c1"><a class="c14" href="#h.92ak2udyfghp">Iteration</a></span></p><p class="c3 c6"><span class="c1"><a class="c14" href="#h.zdhx60a5duyu">Conditions</a></span></p><p class="c3 c6"><span class="c1"><a class="c14" href="#h.ccs6lkxiixw4">Composability of Expressions</a></span></p><p class="c3 c12"><span class="c1"><a class="c14" href="#h.mjv7kyjurri2">JSON Navigation</a></span></p><p class="c3 c6"><span class="c1"><a class="c14" href="#h.xosiyb17dh24">Objects</a></span></p><p class="c3 c6"><span class="c1"><a class="c14" href="#h.mrf42gizl3y6">Arrays</a></span></p><p class="c3 c12"><span class="c1"><a class="c14" href="#h.eb0bxle2oykl">Relational Algebra</a></span></p><p class="c3 c12"><span class="c1"><a class="c14" href="#h.oii6pvliilhi">Access external data</a></span></p><p class="c3 c12"><span class="c1"><a class="c14" href="#h.bm3buqmzhtko">I want more</a></span></p><p class="c3 c6"><span class="c1"><a class="c14" href="#h.5itcphr8262b">Even more</a></span></p><p class="c3 c6"><span class="c1"><a class="c14" href="#h.j7oh3cyhem38">More is not enough</a></span></p><p class="c3 c5"><span></span></p><h1 class="c3"><a name="h.f453h84m2u1a"></a><span>JSON</span></h1><p class="c3 c10"><span>As explained on </span><span class="c1"><a class="c14" href="http://www.json.org/">http://www.json.org/</a></span><span>, JSON is a lightweight data-interchange format designed for humans as well as for computers. It supports as values:</span></p><ol class="c19" start="1"><li class="c7 c3 c6"><span>objects (string-to-value map)</span></li><li class="c7 c3 c6"><span>arrays (ordered sequence of values)</span></li><li class="c7 c3 c6"><span>strings</span></li><li class="c3 c6 c7"><span>numbers</span></li><li class="c7 c3 c6"><span>booleans (true, false)</span></li><li class="c7 c3 c6"><span>null</span></li></ol><p class="c3 c5"><span></span></p><p class="c3"><span>JSONiq provides declarative querying and updating capabilities on JSON data.</span></p><h1 class="c3"><a name="h.qtfakecy3fr7"></a><span>Elevator Pitch</span></h1><p class="c3 c10"><span>JSONiq is based on XQuery, which is a W3C standard (like XML and HTML). XQuery is a very powerful declarative language that manipulates XML data, but that is also a very good fit for JSON. Here is an appetizer before we start the tutorial from scratch.</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">let $stores :=</span></p><p class="c3"><span class="c0">[</span></p><p class="c3"><span class="c0">&nbsp; { &quot;store number&quot; : 1, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c3"><span class="c0">&nbsp; { &quot;store number&quot; : 2, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c3"><span class="c0">&nbsp; { &quot;store number&quot; : 3, &quot;state&quot; : &quot;CA&quot; },</span></p><p class="c3"><span class="c0">&nbsp; { &quot;store number&quot; : 4, &quot;state&quot; : &quot;CA&quot; }</span></p><p class="c3"><span class="c0">]</span></p><p class="c3"><span class="c0">let $sales := [</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;broiler&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 20 &nbsp;},</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 100 },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 50 },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 50 },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 100 },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 150 },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 500 },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 10 },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;shirt&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 10 }</span></p><p class="c3"><span class="c0">]</span></p><p class="c3"><span class="c0">let $join :=</span></p><p class="c3"><span class="c0">&nbsp; for $store in jn:members($stores), $sale in jn:members($sales)</span></p><p class="c3"><span class="c0">&nbsp; where $store(&quot;store number&quot;) = $sale(&quot;store number&quot;)</span></p><p class="c3"><span class="c0">&nbsp; return {</span></p><p class="c3"><span class="c0">&nbsp; &nbsp; &quot;nb&quot; : $store(&quot;store number&quot;),</span></p><p class="c3"><span class="c0">&nbsp; &nbsp; &quot;state&quot; : $store(&quot;state&quot;),</span></p><p class="c3"><span class="c0">&nbsp; &nbsp; &quot;sold&quot; : $sale(&quot;product&quot;)</span></p><p class="c3"><span class="c0">&nbsp; }</span></p><p class="c3"><span class="c0">return [$join]</span></p><p class="c3 c8"><span class="c0 c2">[ </span></p><p class="c3 c8"><span class="c0 c2">&nbsp; { &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;broiler&quot; },</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; { &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; { &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; { &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; { &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; { &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;shirt&quot; }</span></p><p class="c3 c8"><span class="c0 c2">&nbsp;]</span></p><p class="c3 c5"><span></span></p><h1 class="c3"><a name="h.yugk1mszt0re"></a><span>And here you go</span></h1><h2 class="c3"><a name="h.4uvhbej4btrt"></a><span>Like Monsieur Jourdain, you already knew some JSONiq</span></h2><p class="c3 c10"><span>The first thing you need to know is that, as a rule of thumb, a well-formed JSON document is a JSONiq query as well.</span></p><p class="c3 c5"><span></span></p><p class="c3 c10"><span>This means that, most of the time, you can copy-and-paste a JSON document into a query. The following are JSONiq queries that are &quot;idempotent&quot; (they just output themselves):</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">{ &quot;pi&quot; : 3.14, &quot;sq2&quot; : 1.4 }</span></p><p class="c3 c9"><span class="c0 c2">{ &quot;pi&quot; : 3.14, &quot;sq2&quot; : 1.4 }</span></p><p class="c3 c5"><span class="c0 c2"></span></p><p class="c3"><span class="c0">[ 2, 3, 5, 7, 11, 13 ]</span></p><p class="c3 c9"><span class="c0 c2">[ 2, 3, 5, 7, 11, 13 ]</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3"><span class="c0">{</span></p><p class="c3"><span class="c0">&nbsp; &quot;operations&quot; : [</span></p><p class="c3"><span class="c0">&nbsp; &nbsp; { &quot;binary&quot; : [ &quot;and&quot;, &quot;or&quot;] },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp; { &quot;unary&quot; : [&quot;not&quot;] }</span></p><p class="c3"><span class="c0">&nbsp; ],</span></p><p class="c3"><span class="c0">&nbsp; &quot;bits&quot; : [</span></p><p class="c3"><span class="c0">&nbsp; &nbsp; 0, 1</span></p><p class="c3"><span class="c0">&nbsp; ]</span></p><p class="c3"><span class="c0">}</span></p><p class="c3 c8"><span class="c0 c2">{</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; &quot;operations&quot; : [</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; &nbsp; { &quot;binary&quot; : [ &quot;and&quot;, &quot;or&quot; ] },</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; &nbsp; { &quot;unary&quot; : [ &quot;not&quot; ] }</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; ],</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; &quot;bits&quot; : [</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; &nbsp; 0, 1</span></p><p class="c3 c8"><span class="c0 c2">&nbsp; ]</span></p><p class="c3 c8"><span class="c0 c2">}</span></p><p class="c4 c3"><span class="c0 c2"></span></p><p class="c3"><span class="c0">[ { &quot;Question&quot; : &quot;Ultimate&quot; }, [&quot;Life&quot;, &quot;The universe&quot;, &quot;and everything&quot;] ]</span></p><p class="c4 c3"><span class="c0"></span></p><p class="c3 c9"><span class="c0 c2">[ { &quot;Question&quot; : &quot;Ultimate&quot; }, [ &quot;Life&quot;, &quot;The universe&quot;, &quot;and everything&quot; ] ]</span></p><p class="c4 c3"><span class="c0"></span></p><p class="c3"><span>This works with objects, arrays (even nested), strings, numbers, booleans, null. </span><span class="c13">The exceptions to this rule (but we are working on it!) are that:</span></p><p class="c3 c10"><span class="c13">(i) if a pair has a true, false or null value, there must be a space on at least one side of the colon (this will be fixed soon).</span></p><p class="c3 c10"><span class="c13">(ii) empty objects are not recognized and must be written {| |}</span></p><p class="c3 c10"><span class="c13">(iii) characters escaped with the \ in JSON strings are not recognized</span></p><p class="c3 c5"><span class="c13 c18 c0"></span></p><p class="c3 c10"><span>It also works the other way round: if your query outputs an object or an array, you can directly use it as a JSON document.</span></p><p class="c3 c5"><span></span></p><p class="c3 c10"><span>JSONiq is a declarative language. This means that you only need to say </span><span class="c13">what</span><span>&nbsp;you want - the compiler will take care of the </span><span class="c13">how</span><span>. In the above queries, you are basically saying: I want to output this JSON content, and here it is.</span></p><h1 class="c3"><a name="h.1gal1zhyffu4"></a><span>JSONiq basics</span></h1><h2 class="c3"><a name="h.ncbxtez2z3rn"></a><span>The real JSONiq Hello, World!</span></h2><p class="c3"><span>Wondering what a hello world program looks like in JSONiq? Here it is:</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">&quot;Hello, World!&quot;</span></p><p class="c3 c9"><span class="c0 c2">Hello, World!</span></p><p class="c3 c5"><span class="c0 c2"></span></p><p class="c3"><span>Not surprisingly, it outputs the string &quot;Hello, World!&quot;. Again, just say what you want and you will get it.</span></p><h2 class="c3"><a name="h.ifcjheuaxoqc"></a><span>Numbers and arithmetic operations</span></h2><p class="c3 c10"><span>Okay, so, now, you might be thinking: &quot;What is the use of this language if it just outputs what I put in?&quot; Of course, JSONiq can more than that. And still in a declarative way. Here is how it works with numbers:</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">2 + 2</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3"><span>will surprisingly output:</span></p><p class="c3 c9"><span class="c0 c2">4</span></p><p class="c3 c5"><span></span></p><p class="c3"><span>whereas</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">(38 + 2) div 2 + 11 * 2</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3"><span>will output</span></p><p class="c3 c9"><span class="c0 c2">42</span></p><p class="c3 c5"><span class="c0 c2"></span></p><p class="c3 c10"><span>(mind the division operator which is the &quot;div&quot; keyword. The slash has other semantics).</span></p><p class="c3 c5"><span></span></p><p class="c3"><span>Like JSON, JSONiq works with decimals, too:</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">6.022e23 * 42</span></p><p class="c3 c9"><span class="c0 c2">2.52924E25</span></p><h2 class="c3"><a name="h.jmbc6c1fqqwx"></a><span>Logical operations</span></h2><p class="c3 c10"><span>JSONiq supports boolean operations.</span></p><p class="c3 c10 c5"><span></span></p><p class="c3"><span class="c0">true and false</span></p><p class="c3 c9"><span class="c0 c2">false</span></p><p class="c4 c3"><span class="c0 c2"></span></p><p class="c3"><span class="c0">(true or false) and (false or true)</span></p><p class="c3 c9"><span class="c0 c2">true</span></p><p class="c4 c3"><span class="c0 c2"></span></p><p class="c3"><span>The unary not is a function (yes, JSONiq has functions, and they are in the same way as in many languages):</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3"><span class="c0">not(true)</span></p><p class="c3 c9"><span class="c0 c2">false</span></p><h2 class="c3"><a name="h.iq1ixg5ywx0r"></a><span>Strings</span></h2><p class="c3"><span>JSONiq is capable of manipulating strings as well, using functions:</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">concat(&quot;Hello &quot;, &quot;Captain Kirk&quot;)</span></p><p class="c3 c9"><span class="c0 c2">Hello Captain Kirk</span></p><p class="c4 c3"><span class="c0 c2"></span></p><p class="c3"><span class="c0">substring(&quot;Mister Spock&quot;, 8, 5)</span></p><p class="c3 c9"><span class="c0 c2">Spock</span></p><p class="c4 c3"><span class="c2"></span></p><p class="c3 c10"><span>JSONiq comes up with a rich string function library out of the box inherited from its base language. These functions are listed </span><span class="c1"><a class="c14" href="http://www.w3.org/TR/xpath-functions-30/#string-functions">here</a></span><span>&nbsp;(actually, you will find even many more for numbers, etc.)</span></p><h2 class="c3"><a name="h.rv6w8p4wenfa"></a><span>Sequences</span></h2><p class="c3"><span>Until now, we only handled single values (an object, an array, a number, a string, a boolean). JSONiq supports sequences of values. You can build a sequence using commas:</span></p><p class="c3 c5"><span></span></p><p class="c3"><span>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</span></p><p class="c3 c9"><span class="c2">1 2 3 4 5 6 7 8 9 10</span></p><p class="c3 c5"><span class="c2"></span></p><p class="c3"><span class="c0">1, </span><span class="c0">true</span><span class="c0">, 4.2e1, &quot;Life&quot;</span></p><p class="c3 c9"><span class="c0 c2">1 true 42 Life</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3"><span>The &quot;to&quot; operator is very convenient, too:</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3"><span class="c0">1 to 100</span></p><p class="c3 c9"><span class="c2">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20</span></p><p class="c3 c9"><span class="c2">21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40</span></p><p class="c3 c9"><span class="c2">41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60</span></p><p class="c3 c9"><span class="c2">61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80</span></p><p class="c3 c9"><span class="c2">81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100</span></p><p class="c3 c5"><span></span></p><p class="c3"><span>Some functions even work on sequences:</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3"><span class="c0">sum(1 to 100)</span></p><p class="c3 c9"><span class="c0 c2">5050</span></p><p class="c4 c3"><span class="c0 c2"></span></p><p class="c3"><span class="c0">string-join((&quot;These&quot;, &quot;are&quot;, &quot;some&quot;, &quot;words&quot;), &quot;-&quot;)</span></p><p class="c3 c9"><span class="c0 c2">These-are-some-words</span></p><p class="c4 c3"><span class="c0 c2"></span></p><p class="c3"><span class="c0">count(10 to 20)</span></p><p class="c3 c9"><span class="c0 c2">11</span></p><p class="c3 c5"><span class="c0 c2"></span></p><p class="c3"><span class="c0">avg(1 to 100)</span></p><p class="c3 c9"><span class="c0 c2">50.5</span></p><h1 class="c3"><a name="h.7x2r8sj1zjwe"></a><span>A bit more in depth</span></h1><h2 class="c3"><a name="h.b6c9zq9um8cu"></a><span>Variables</span></h2><p class="c3"><span>You can bind a sequence of values to a (dollar-prefixed) variable, like so:</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">let $x := &quot;Bearing 3 1 4 Mark 5. &quot;</span></p><p class="c3"><span class="c0">return concat($x, &quot;Engage!&quot;)</span></p><p class="c3 c9"><span class="c0 c2">Bearing 3 1 4 Mark 5. Engage!</span></p><p class="c4 c3"><span class="c0 c2"></span></p><p class="c4 c3"><span class="c0"></span></p><p class="c3"><span class="c0">let $x := (&quot;Kirk&quot;, &quot;Picard&quot;, &quot;Sisko&quot;)</span></p><p class="c3"><span class="c0">return string-join($x, &quot; and &quot;)</span></p><p class="c3 c9"><span class="c0 c2">Kirk and Picard and Sisko</span></p><p class="c3 c5"><span class="c0 c2"></span></p><p class="c3"><span>You can bind as many variables as you want:</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">let $x := 1</span></p><p class="c3"><span class="c0">let $y := $x * 2</span></p><p class="c3"><span class="c0">let $z := $y + $x</span></p><p class="c3"><span class="c0">return [$x, $y, $z]</span></p><p class="c3 c9"><span class="c0 c2">[ 1, 2, 3 ]</span></p><p class="c4 c3"><span class="c0 c2"></span></p><p class="c3"><span>and even reuse the same name:</span></p><p class="c3"><span class="c0">let $x := 1</span></p><p class="c3"><span class="c0">let $x := $x + 2</span></p><p class="c3"><span class="c0">let $x := $x + 3</span></p><p class="c3"><span class="c0">return $x</span></p><p class="c3 c9"><span class="c0 c2">6</span></p><h2 class="c3"><a name="h.92ak2udyfghp"></a><span>Iteration</span></h2><p class="c3 c10"><span>In a way very similar to let, you can iterate over a sequence of values with the &quot;for&quot; keyword. Instead of binding the entire sequence of the variable, it will bind each value of the sequence in turn to this variable.</span></p><p class="c3 c10 c5"><span></span></p><p class="c3 c10"><span class="c0">for $i in 1 to 10</span></p><p class="c3 c10"><span class="c0">return $i * 2</span></p><p class="c3 c9"><span class="c0 c2">2 4 6 8 10 12 14 16 18 20</span></p><p class="c4 c3"><span class="c0 c2"></span></p><p class="c3"><span>More interestingly, you can combine fors and lets like so:</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3"><span class="c0">let $sequence := 1 to 10</span></p><p class="c3"><span class="c0">for $value in $sequence</span></p><p class="c3"><span class="c0">let $square := $value * 2</span></p><p class="c3"><span class="c0">return $square</span></p><p class="c3 c9"><span class="c0 c2">2 4 6 8 10 12 14 16 18 20</span></p><p class="c3 c4"><span class="c0 c2"></span></p><p class="c3"><span>and even filter out some values:</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3"><span class="c0">let $sequence := 1 to 10</span></p><p class="c3"><span class="c0">for $value in $sequence</span></p><p class="c3"><span class="c0">let $square := $value * 2</span></p><p class="c3"><span class="c0">where $square &lt; 10</span></p><p class="c3"><span class="c0">return $square</span></p><p class="c3 c9"><span class="c0 c2">2 4 6 8</span></p><h2 class="c3"><a name="h.zdhx60a5duyu"></a><span>Conditions</span></h2><p class="c3"><span>You can make the output depend on a condition with an if-then-else construct:</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">for $x in 1 to 10</span></p><p class="c3"><span class="c0">return if ($x &lt; 5) then $x</span></p><p class="c3"><span class="c0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else -$x</span></p><p class="c3 c9"><span class="c0 c2">1 2 3 4 -5 -6 -7 -8 -9 -10</span></p><h2 class="c3"><a name="h.ccs6lkxiixw4"></a><span>Composability of Expressions</span></h2><p class="c3"><span>Now that you know of a couple of elementary JSONiq expressions, you can combine then in more elaborate expressions. For example, you can put any sequence of values in an array:</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">[ 1 to 10 ]</span></p><p class="c3 c9"><span class="c0 c2">[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]</span></p><p class="c4 c3"><span class="c0 c2"></span></p><p class="c3"><span>Or you can dynamically compute the value of object pairs:</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">{</span></p><p class="c3"><span class="c0">&nbsp; &quot;Greeting&quot; : (let $d := &quot;Mister Spock&quot;</span></p><p class="c3"><span class="c0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return concat(&quot;Hello, &quot;, $d)),</span></p><p class="c3"><span class="c0">&nbsp; &quot;Farewell&quot; : string-join((&quot;Live&quot;, &quot;long&quot;, &quot;and&quot;, &quot;prosper&quot;),</span></p><p class="c3"><span class="c0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot; &quot;)</span></p><p class="c3"><span class="c0">}</span></p><p class="c3 c5"><span class="c0 c2"></span></p><p class="c3 c9"><span class="c0 c2">{ &quot;Greeting&quot; : &quot;Hello, Mister Spock&quot;, &quot;Farewell&quot; : &quot;Live long and prosper&quot; }</span></p><p class="c3 c5"><span class="c2"></span></p><p class="c3"><span>You can also dynamically generate object singletons (with a single pair):</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3"><span class="c0">{ concat(&quot;Integer &quot;, 2) : 2 * 2 }</span></p><p class="c3 c9"><span class="c0 c2">{ &quot;Integer 2&quot; : 4 }</span></p><p class="c4 c3"><span class="c0 c2"></span></p><p class="c3"><span>and then merge lots of them into a new object with the {| |} notation:</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3"><span class="c0">{|</span></p><p class="c3"><span class="c0">&nbsp; for $i in 1 to 10</span></p><p class="c3"><span class="c0">&nbsp; return { concat(&quot;Square of &quot;, $i) : $i * $i }</span></p><p class="c3"><span class="c0">|}</span></p><p class="c3 c8"><span class="c0 c2">{</span></p><p class="c3 c8"><span class="c0 c2">&quot;Square of 1&quot; : 1,</span></p><p class="c3 c8"><span class="c0 c2">&quot;Square of 2&quot; : 4,</span></p><p class="c3 c8"><span class="c0 c2">&quot;Square of 3&quot; : 9,</span></p><p class="c3 c8"><span class="c0 c2">&quot;Square of 4&quot; : 16,</span></p><p class="c3 c8"><span class="c0 c2">&quot;Square of 5&quot; : 25,</span></p><p class="c3 c8"><span class="c0 c2">&quot;Square of 6&quot; : 36,</span></p><p class="c3 c8"><span class="c0 c2">&quot;Square of 7&quot; : 49,</span></p><p class="c3 c8"><span class="c0 c2">&quot;Square of 8&quot; : 64,</span></p><p class="c3 c8"><span class="c0 c2">&quot;Square of 9&quot; : 81,</span></p><p class="c3 c8"><span class="c0 c2">&quot;Square of 10&quot; : 100</span></p><p class="c3 c8"><span class="c0 c2">}</span></p><h1 class="c3"><a name="h.mjv7kyjurri2"></a><span>JSON Navigation</span></h1><p class="c3 c10"><span>Up to now, you learnt how to compose expressions so as to do some computations and to build objects and arrays. It also works the other way round: if you have some JSON data, you can access it and navigate.</span></p><p class="c3 c10 c5"><span></span></p><p class="c3 c10"><span>All you need to know is: JSONiq views</span></p><ol class="c19" start="1"><li class="c7 c3 c6"><span>an array as a sequence of values,</span></li><li class="c7 c3 c6"><span>an object as a set of name/value pairs</span></li></ol><h2 class="c3 c10"><a name="h.xosiyb17dh24"></a><span>Objects</span></h2><p class="c3 c10"><span>If you use an object as a functor and provide it with a string, it will return the value associated with its parameter:</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3"><span class="c0">let $person := {</span></p><p class="c3"><span class="c0">&nbsp; &quot;first name&quot; : &quot;Sarah&quot;,</span></p><p class="c3"><span class="c0">&nbsp; &quot;age&quot; : 13,</span></p><p class="c3"><span class="c0">&nbsp; &quot;gender&quot; : &quot;female&quot;,</span></p><p class="c3"><span class="c0">&nbsp; &quot;friends&quot; : [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c3"><span class="c0">}</span></p><p class="c3"><span class="c0">return $person(&quot;first name&quot;)</span></p><p class="c3 c9"><span class="c0 c2">&quot;Sarah&quot;</span></p><p class="c4 c3"><span class="c0 c2"></span></p><p class="c3"><span>You can also ask for all keys in an object:</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">let $person := {</span></p><p class="c3"><span class="c0">&nbsp; &quot;name&quot; : &quot;Sarah&quot;,</span></p><p class="c3"><span class="c0">&nbsp; &quot;age&quot; : 13,</span></p><p class="c3"><span class="c0">&nbsp; &quot;gender&quot; : &quot;female&quot;,</span></p><p class="c3"><span class="c0">&nbsp; &quot;friends&quot; : [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c3"><span class="c0">}</span></p><p class="c3"><span class="c0">return { &quot;keys&quot; : [ jn:keys($person)] }</span></p><p class="c16 c4 c3"><span class="c0 c2"></span></p><p class="c3 c9 c16"><span class="c0 c2">{ &quot;keys&quot; : [ &quot;name&quot;, &quot;age&quot;, &quot;gender&quot;, &quot;friends&quot; ] }</span></p><h2 class="c3"><a name="h.mrf42gizl3y6"></a><span>Arrays</span></h2><p class="c3 c10"><span>If you use an array as a functor and provide it with an integer, it will return the corresponding entry:</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3"><span class="c0">let $friends := [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c3"><span class="c0">return $friends(2)</span></p><p class="c3 c9"><span class="c0 c2">Mary</span></p><p class="c3 c5"><span></span></p><p class="c3"><span>It is also possible to get the size of an array:</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">let $person := {</span></p><p class="c3"><span class="c0">&nbsp; &quot;name&quot; : &quot;Sarah&quot;,</span></p><p class="c3"><span class="c0">&nbsp; &quot;age&quot; : 13,</span></p><p class="c3"><span class="c0">&nbsp; &quot;gender&quot; : &quot;female&quot;,</span></p><p class="c3"><span class="c0">&nbsp; &quot;friends&quot; : [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c3"><span class="c0">}</span></p><p class="c3"><span class="c0">return { &quot;how many friends&quot; : jn:size($person(&quot;friends&quot;)) }</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3 c9"><span class="c0 c2">{ &quot;how many friends&quot; : 3 }</span></p><p class="c3 c5"><span></span></p><p class="c3"><span>For convenience, there is a function that returns all elements in an array, as a sequence:</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">let $person := {</span></p><p class="c3"><span class="c0">&nbsp; &quot;name&quot; : &quot;Sarah&quot;,</span></p><p class="c3"><span class="c0">&nbsp; &quot;age&quot; : 13,</span></p><p class="c3"><span class="c0">&nbsp; &quot;gender&quot; : &quot;female&quot;,</span></p><p class="c3"><span class="c0">&nbsp; &quot;friends&quot; : [ &quot;Jim&quot;, &quot;Mary&quot;, &quot;Jennifer&quot;]</span></p><p class="c3"><span class="c0">}</span></p><p class="c3"><span class="c0">return jn:members($person(&quot;friends&quot;))</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3 c9"><span class="c0 c2">Jim Mary Jennifer</span></p><p class="c4 c3"><span class="c2"></span></p><h1 class="c3"><a name="h.eb0bxle2oykl"></a><span>Relational Algebra</span></h1><p class="c3 c10"><span>Remember last century&#39;s SELECT FROM WHERE statements? Well, JSONiq didn&#39;t throw out the baby with the bathwater. It has selection, projection and join capability.</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">let $stores :=</span></p><p class="c3"><span class="c0">[</span></p><p class="c3"><span class="c0">&nbsp; { &quot;store number&quot; : 1, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c3"><span class="c0">&nbsp; { &quot;store number&quot; : 2, &quot;state&quot; : &quot;MA&quot; },</span></p><p class="c3"><span class="c0">&nbsp; { &quot;store number&quot; : 3, &quot;state&quot; : &quot;CA&quot; },</span></p><p class="c3"><span class="c0">&nbsp; { &quot;store number&quot; : 4, &quot;state&quot; : &quot;CA&quot; }</span></p><p class="c3"><span class="c0">]</span></p><p class="c3"><span class="c0">let $sales := [</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;broiler&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 20 &nbsp;},</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 100 },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 50 },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;toaster&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 50 },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 100 },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;blender&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 150 },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 1, &quot;quantity&quot; : 500 },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;socks&quot;, &quot;store number&quot; : 2, &quot;quantity&quot; : 10 },</span></p><p class="c3"><span class="c0">&nbsp; &nbsp;{ &quot;product&quot; : &quot;shirt&quot;, &quot;store number&quot; : 3, &quot;quantity&quot; : 10 }</span></p><p class="c3"><span class="c0">]</span></p><p class="c3"><span class="c0">let $join :=</span></p><p class="c3"><span class="c0">&nbsp; for $store in jn:members($stores), $sale in jn:members($sales)</span></p><p class="c3"><span class="c0">&nbsp; where $store(&quot;store number&quot;) = $sale(&quot;store number&quot;)</span></p><p class="c3"><span class="c0">&nbsp; return {</span></p><p class="c3"><span class="c0">&nbsp; &nbsp; &quot;nb&quot; : $store(&quot;store number&quot;),</span></p><p class="c3"><span class="c0">&nbsp; &nbsp; &quot;state&quot; : $store(&quot;state&quot;),</span></p><p class="c3"><span class="c0">&nbsp; &nbsp; &quot;sold&quot; : $sale(&quot;product&quot;)</span></p><p class="c3"><span class="c0">&nbsp; }</span></p><p class="c3"><span class="c0">return [$join]</span></p><p class="c3 c8"><span class="c0 c2">[ </span></p><p class="c3 c8"><span class="c0 c2">{ &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;broiler&quot; },</span></p><p class="c3 c8"><span class="c0 c2">{ &quot;nb&quot; : 1, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c3 c8"><span class="c0 c2">{ &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c3 c8"><span class="c0 c2">{ &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c3 c8"><span class="c0 c2">{ &quot;nb&quot; : 2, &quot;state&quot; : &quot;MA&quot;, &quot;sold&quot; : &quot;socks&quot; },</span></p><p class="c3 c8"><span class="c0 c2">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;toaster&quot; },</span></p><p class="c3 c8"><span class="c0 c2">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c3 c8"><span class="c0 c2">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;blender&quot; },</span></p><p class="c3 c8"><span class="c0 c2">{ &quot;nb&quot; : 3, &quot;state&quot; : &quot;CA&quot;, &quot;sold&quot; : &quot;shirt&quot; }</span></p><p class="c3 c8"><span class="c0 c2">&nbsp;]</span></p><h1 class="c3"><a name="h.oii6pvliilhi"></a><span>Access external data</span></h1><p class="c3"><span>Our implementation even supports collections of JSON objects or arrays:</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">dml:collection(&quot;my:data&quot;)</span></p><p class="c3 c9"><span class="c0 c2">{ &quot;foo&quot; : &quot;Your&quot; }</span></p><p class="c3 c9"><span class="c0 c2">{ &quot;foo&quot; : &quot;Collection&quot; }</span></p><p class="c3 c9"><span class="c0 c2">{ &quot;foo&quot; : &quot;of&quot; }</span></p><p class="c3 c9"><span class="c0 c2">{ &quot;foo&quot; : &quot;JSON&quot; }</span></p><p class="c3 c9"><span class="c0 c2">{ &quot;foo&quot; : &quot;objects&quot; }</span></p><p class="c4 c3"><span class="c0 c2"></span></p><p class="c3"><span>It is also possible to get JSON content with an HTTP request, or by parsing it from a string. Functions that handle this are available.</span></p><h1 class="c3"><a name="h.bm3buqmzhtko"></a><span>I want more</span></h1><p class="c3 c10"><span>JSONiq supports JSON updates. You can declaratively update your JSON data. JSONiq provides updating expressions. The list of updates that is eventually output by your program is then applied to your JSON data.</span></p><p class="c3 c5"><span></span></p><p class="c3"><span class="c0">copy $people := {</span></p><p class="c3"><span class="c0">&nbsp; &quot;John&quot; : { &quot;status&quot; : &quot;single&quot; },</span></p><p class="c3"><span class="c0">&nbsp; &quot;Mary&quot; : { &quot;status&quot; : &quot;single&quot; } }</span></p><p class="c3"><span class="c0">modify (replace json value of $people(&quot;John&quot;)(&quot;status&quot;) with &quot;married&quot;,</span></p><p class="c3"><span class="c0">&nbsp; &nbsp; &nbsp; &nbsp; replace json value of $people(&quot;Mary&quot;)(&quot;status&quot;) with &quot;married&quot;)</span></p><p class="c3"><span class="c0">return $people</span></p><p class="c3 c5"><span class="c0"></span></p><p class="c3 c9"><span class="c0 c2">{ &quot;John&quot; : { &quot;status&quot; : &quot;married&quot; }, &quot;Mary&quot; : { &quot;status&quot; : &quot;married&quot; } }</span></p><p class="c4 c3"><span class="c0 c2"></span></p><p class="c3 c10"><span>Other updates are insertion into an object or an array, replacement of a value in an object or an array, deletion in an object or an array, renaming an object pair, appending to an array.</span></p><h2 class="c3"><a name="h.5itcphr8262b"></a><span>Even more</span></h2><p class="c3"><span>JSONiq can do way more that what is presented here. Here are a couple of highlights:</span></p><p class="c3 c10"><span>- JSONiq is a strongly typed language, but is smart enough to not bother you with types when unnecessary. It potentially supports static typing as well to detect errors before you even execute your program.</span></p><p class="c3"><span>- You can define your own functions and modules.</span></p><p class="c3"><span>- JSONiq provides you with loads of available modules.</span></p><p class="c3 c10"><span>- JSONiq has tons of further features such as switch, typeswitch and try-catch expressions, universal/existential quantifiers, path expressions, filtering expressions, functors, mappings, grouping, windowing.</span></p><h2 class="c3"><a name="h.j7oh3cyhem38"></a><span>More is not enough</span></h2><p class="c3 c10"><span>- JSONiq supports XML. Yes: you can manipulate JSON and XML with the same language! JSONiq is actually a superset of XQuery, a W3C standard, and extends its data model to support JSON.</span></p><p class="c3 c10"><span>- JSONiq supports scripting. If you need to write a full-fledged, side-effecting Web application, scripting is for you.</span></p><p class="c3 c10 c5"><span></span></p><p class="c3 c10"><span>The complete JSONiq specification is available on </span><span class="c1"><a class="c14" href="http://www.jsoniq.org/">http://www.jsoniq.org/</a></span></p></body></html>
17\ No newline at end of file

Subscribers

People subscribed via source and target branches