Product SiteDocumentation Site

Chapter 5. Expressions

5.1. Construction of items
5.1.1. Atomic literals
5.1.2. Object constructors
5.1.3. Array constructors
5.1.4. Composing constructors
5.2. Basic operations
5.2.1. Construction of sequences
5.2.2. Parenthesized expression
5.2.3. Arithmetics
5.2.4. String concatenation
5.2.5. Comparison
5.2.6. Logics
5.3. Function Calls
5.4. Selectors
5.4.1. Object field selector
5.4.2. Array member selector
5.4.3. Sequence predicates
5.5. Control flow expressions
5.5.1. Conditional expressions
5.5.2. Switch expressions
5.5.3. Try-catch expressions
5.6. FLWOR expressions
5.6.1. For clauses
5.6.2. Where clauses
5.6.3. Order clauses
5.6.4. Group clauses
5.6.5. Let clauses
5.6.6. Count clauses
5.6.7. Map operator
5.6.8. Composing FLWOR expressions
5.6.9. Ordered and Unordered expressions
5.7. Expressions dealing with types
5.7.1. Instance-of expressions
5.7.2. Treat expressions
5.7.3. Castable expressions
5.7.4. Cast expressions
5.7.5. Typeswitch expressions

5.1. Construction of items

This will not come as a surprise since JSONiq is tailor-made: the items mentioned in the former section are constructed... exactly as they are constructed in JSON. Yes, you already know some JSONiq: any JSON document is also a valid JSONiq query which just "returns itself"!

5.1.1. Atomic literals

Literal
Figure 5.1. Literal

NumericLiteral
Figure 5.2. NumericLiteral

IntegerLiteral
Figure 5.3. IntegerLiteral

DecimalLiteral
Figure 5.4. DecimalLiteral

DoubleLiteral
Figure 5.5. DoubleLiteral

StringLiteral
Figure 5.6. StringLiteral

BooleanLiteral
Figure 5.7. BooleanLiteral

NullLiteral
Figure 5.8. NullLiteral

The syntax for creating strings, numbers, booleans and nulls is identical to that of JSON (it is actually a more flexible superset, for example leading 0s are allowed, and a decimal literal can begin with a dot). Note that JSONiq distinguishes between integers (no dot, no scientific notation), decimals (dot but no scientific notation) and doubles (scientific notation). As expected, an integer literal creates an atomic of type integer, and so on. No surprises. JSON's backslash escaping is supported in string literals. Also, like in JSON, double quotes are required and single quotes are forbidden.
Example 5.1. String literals
"foo"
Result (run with Zorba):
foo

Example 5.2. String literals with escaping
"This is a line\nand this is a new line"
Result (run with Zorba):
This is a line and this is a new line

Example 5.3. String literals with Unicode character escaping
"\u0001"
Result (run with Zorba):


Example 5.4. String literals with a nested quote
"This is a nested \"quote\""
Result (run with Zorba):
This is a nested "quote"

Example 5.5. Integer literals
42
Result (run with Zorba):
42

Example 5.6. Decimal literals
3.14
Result (run with Zorba):
3.14

Example 5.7. Double literals
+6.022E23
Result (run with Zorba):
6.022E23

Example 5.8. Boolean literals (true)
true
Result (run with Zorba):
true

Example 5.9. Boolean literals (false)
false
Result (run with Zorba):
false

Example 5.10. Null literals
null
Result (run with Zorba):
null

5.1.2. Object constructors

ObjectConstructor
Figure 5.9. ObjectConstructor

PairConstructor
Figure 5.10. PairConstructor

The syntax for creating objects is identical to that of JSON. You can use for an object key any string literal, and for an object value any literal, object constructor or array constructor.
Example 5.11. Empty object constructors
{}
Result (run with Zorba):
{ }

Example 5.12. Object constructors 1
{ "foo" : "bar" }
Result (run with Zorba):
{ "foo" : "bar" }

Example 5.13. Object constructors 2
{ "foo" : [ 1, 2, 3, 4, 5, 6 ] }
Result (run with Zorba):
{ "foo" : [ 1, 2, 3, 4, 5, 6 ] }

Example 5.14. Object constructors 3
{ "foo" : true, "bar" : false }
Result (run with Zorba):
{ "foo" : true, "bar" : false }

Example 5.15. Nested object constructors
{ "this is a key" : { "value" : "a value" } }
Result (run with Zorba):
{ "this is a key" : { "value" : "a value" } }

Oh, and like in JavaScript, if your key is simple enough (like alphanumerics, underscores, dashes, this kind of things), you are welcome to omit the quotes. The strings for which quotes are not mandatory are called NCNames. This class of strings can be used for unquoted keys, for variable and function names, and for module aliases.
Example 5.16. Object constructors with unquoted key 1
{ foo : "bar" }
Result (run with Zorba):
{ "foo" : "bar" }

Example 5.17. Object constructors with unquoted key 2
{ foo : [ 1, 2, 3, 4, 5, 6 ] }
Result (run with Zorba):
{ "foo" : [ 1, 2, 3, 4, 5, 6 ] }

Example 5.18. Object constructors with unquoted key 3
{ foo : "bar", bar : "foo" }
Result (run with Zorba):
{ "foo" : "bar", "bar" : "foo" }

Example 5.19. Object constructors with needed quotes around the key
{ "but you need the quotes here" : null }
Result (run with Zorba):
{ "but you need the quotes here" : null }

5.1.3. Array constructors

ArrayConstructor
Figure 5.11. ArrayConstructor

Expr
Figure 5.12. Expr

The syntax for creating arrays is identical to that of JSON: square brackets, comma separated literals, object constructors and arrays constructors.
Example 5.20. Empty array constructors
[]
Result (run with Zorba):
[ ]

Example 5.21. Array constructors
[ 1, 2, 3, 4, 5, 6 ]
Result (run with Zorba):
[ 1, 2, 3, 4, 5, 6 ]

Example 5.22. Nested array constructors
[ "foo", 3.14, [ "Go", "Boldly", "When", "No", "Man", "Has", "Gone", "Before" ], { "foo" : "bar" }, true, false, null ]
Result (run with Zorba):
[ "foo", 3.14, [ "Go", "Boldly", "When", "No", "Man", "Has", "Gone", "Before" ], { "foo" : "bar" }, true, false, null ]

Square brackets are mandatory. Do not push it.

5.1.4. Composing constructors

Of course, JSONiq would not be very interesting if all you could do is copy and paste JSON documents.
Because JSONiq expressions are fully composable, in objects and arrays constructors, you can put way more than just atomic literals, object constructors and array constructors: you can put any JSONiq expression.
The following examples are just a few of many operators you can use: "to" for creating arithmetic sequences, "||" for concatenating strings, "+" for adding numbers, "," for appending sequences.
In an array, the operand expression will evaluated to a sequence of items, and these items will be copied and become members of the newly created array.
Example 5.23. Composable array constructors
  [ 1 to 10 ]
Result (run with Zorba):
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

In an object, the expression you use for the key must evaluate to an atomic - if it is not a string, it will just be cast to it.
Example 5.24. Composable object keys
  { "foo" || "bar" : true }
Result (run with Zorba):
{ "foobar" : true }

An error is raised if the key expressions is not an atomic.
Example 5.25. Non-atomic object keys
  { [ 1, 2 ] : true }
Result (run with Zorba):
An error was raised: can not atomize an array item: an array has probably been passed where an atomic value is expected (e.g., as a key, or to a function expecting an atomic item)

And do not worry about the value expression: if it is is empty, null will be used as a value, and if it contains two items or more, they will be wrapped into an array.
If the colon is preceded with a question mark, then the pair will be omitted if the value expression evaluates to the empty sequence.
Example 5.26. Composable object values
  { "foo" : 1 + 1 }
Result (run with Zorba):
{ "foo" : 2 }

Example 5.27. Composable object values and automatic conversion
  { "foo" : (), "bar" : (1, 2) }
Result (run with Zorba):
{ "foo" : null, "bar" : [ 1, 2 ] }

Example 5.28. Optional pair (not implemented yet in Zorba)
  { "foo" ?: (), "bar" : (1, 2) }
Result (run with Zorba):
An error was raised: invalid expression: syntax error, unexpected "?", expecting "end of file" or "," or "}"

The {| |} syntax can be used to merge several objects.
Example 5.29. Merging object constructor
  {| { "foo" : "bar" }, { "bar" : "foo" } |}
Result (run with Zorba):
{ "foo" : "bar", "bar" : "foo" }

An error is raised if the operand expression does not evaluate to a sequence of objects.
Example 5.30. Merging object constructor with a type error
  {| 1 |}
Result (run with Zorba):
An error was raised: xs:integer can not be treated as type object()*