Product SiteDocumentation Site

Chapter 4. Construction of JSON values

4.1. Array Constructors
4.2. Object Constructors
4.3. Strings
4.4. Numbers
4.5. Booleans
4.6. Null
4.7. Boolean and null literals
JSONiq Constructors create Objects, and Arrays. If an Object Constructor or an Array Constructor contains only literal data, as a rule of thumb, the syntax is the same as the JSON serialization. Constructors can also contain XQuery expressions to create content as the result of a query.
Here is the syntax for object and array constructors:
      PrimaryExpr ::= -- everything so far --
        | JSONConstructor

      JSONConstructor ::=
          ArrayConstructor
        | ObjectConstructor

      ArrayConstructor ::= "[" Expr? "]"

      ObjectConstructor ::= 
          "{" ( PairConstructor ("," PairConstructor)* )? "}"
        | | "{|" Expr "|}"
      PairConstructor ::=  ExprSingle (":" | "?:") ExprSingle
XQuery expressions can occur within JSON constructors, and JSON constructors can occur within XQuery expressions. The semantics of such expressions are discussed in Chapter 9, Combining XML and JSON.

4.1. Array Constructors

The expression in an Array Constructor, if present, evaluates to a sequence of zero or more items. The members of the constructed array are copies of these items, in the same order.
Example 4.1. Array Constructors
An Array Constructor:
        [ "Sunday",
          "Monday",
          "Tuesday",
          "Wednesday",
          "Thursday",
          "Friday",
          "Saturday" ]
Arrays can nest.
          [
            [1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]
          ]
Combining an Array Constructor with XQuery expressions:
Query:
        [ 10 to 15 ]
Result:
        [ 10, 11, 12, 13, 14, 15 ]