Product SiteDocumentation Site

4.2. Object Constructors

An Object Constructor is made of Pair Constructors. Each Pair Constructor creates a single string/item pair as follows:
  • The string is the result of evaluating the left operand, atomizing it, checking that it results in a single atomic item, and casting it to xs:string. Errors such as jerr:JNTY0004 or err:XPTY0004 may be raised upon failure of one of these steps.
  • If the right operand evaluates to a single item, the value of the pair is a copy of this result.
    If the right operand evaluates to the empty sequence, the value of the pair is the atomic value null (of type js:null). However, if the colon is preceded with a question mark, the pair is omitted instead.
    If the right operand evaluates to a sequence of more than one item, the value of the pair is an array containing copies of all items in this sequence, in the same order.
An object is constructed, the pairs property of which comprise all pairs generated by the pair constructors. An error jerr:JNDY0003 is raised if two pairs have the same name (the comparison is made using fn:codepoint-equal).
Example 4.2. Object Constructors
An Object Constructor with literal data:
          {
            "id" : 404,
            "name" : "Stanco Grease Pot",
            "price" : 6.49,
            "weight" : 3.8,
            "uses" : ["Grease storage","Backpacking pot"]
          }
Combining an Object Constructor with XQuery expressions:
Query:
          {
            "Sunday" : 1,
            "Monday" : 1 + 1,
            "Tuesday" : 3 * 1,
            "Wednesday" : 8 div 2,
            "Thursday" : 5,
            "Friday" : count(for $i in 1 to 6 return $i),
            "Saturday" : 10 - 3,
            "NotADay" ?: ()
          }
Result:
          {
            "Sunday" : 1,
            "Monday" : 2,
            "Tuesday" : 3,
            "Wednesday" : 4,
            "Thursday" : 5,
            "Friday" : 6,
            "Saturday" : 7
          }

Note:
A JSON item cannot be a child of an XML element or attribute. If a JSON item is used in the content expression of an XQuery constructor, the result will raise an error, as described in Chapter 9, Combining XML and JSON.
There is also a syntax for dynamic object construction, which merges all the objects returned by the inner expression into a single object with a so-called "simple object union". A simple object union creates a new object, the pairs property of which is obtained by accumulating the pairs of all operand objects. An error jerr:JNDY0003 is raised if two pairs with the same name are encountered.
Example 4.3. Dynamically building an object
Query:
          let $object1 := { "Captain" : "Kirk" }
          let $object2 := { "First officer" : "Spock" }
          return {| $object1, $object2 |}
Result:
          {
            "Captain" : "Kirk",
            "First officer" : "Spock"
          }
Query:
          {|
            for $d at $i in ("Sunday",
                             "Monday",
                             "Tuesday",
                             "Wednesday",
                             "Thursday",
                             "Friday",
                             "Saturday" )
            return { $d : $i }
          |}
Result:
          {
            "Sunday" : 1,
            "Monday" : 2,
            "Tuesday" : 3,
            "Wednesday" : 4,
            "Thursday" : 5,
            "Friday" : 6,
            "Saturday" : 7
          }