Product SiteDocumentation Site

5.5. Control flow expressions

JSONiq supports control flow expressions such as if-then-else, switch and typeswitch.

5.5.1. Conditional expressions

IfExpr
Figure 5.33. IfExpr

A conditional expressions allows you to pick one or another value depending on a boolean value.
Example 5.84. A conditional expression
if (1 + 1 eq 2) then { "foo" : "yes" } else { "foo" : "false" }
Result (run with Zorba):
{ "foo" : "yes" }

The behavior of the expression inside the if is similar to that of logical operations (two-valued logics), meaning that non-boolean values get converted to a boolean.
Example 5.85. A conditional expression
if (null) then { "foo" : "yes" } else { "foo" : "no" }
Result (run with Zorba):
{ "foo" : "no" }

Example 5.86. A conditional expression
if (1) then { "foo" : "yes" } else { "foo" : "no" }
Result (run with Zorba):
{ "foo" : "yes" }

Example 5.87. A conditional expression
if (0) then { "foo" : "yes" } else { "foo" : "no" }
Result (run with Zorba):
{ "foo" : "no" }

Example 5.88. A conditional expression
if ("foo") then { "foo" : "yes" } else { "foo" : "no" }
Result (run with Zorba):
{ "foo" : "yes" }

Example 5.89. A conditional expression
if ("") then { "foo" : "yes" } else { "foo" : "no" }
Result (run with Zorba):
{ "foo" : "no" }

Example 5.90. A conditional expression
if (()) then { "foo" : "yes" } else { "foo" : "no" }
Result (run with Zorba):
{ "foo" : "no" }

Example 5.91. A conditional expression
if (({ "foo" : "bar" }, [ 1, 2, 3, 4])) then { "foo" : "yes" } else { "foo" : "no" }
Result (run with Zorba):
{ "foo" : "yes" }

Note that the else clause is mandatory (but can be the empty sequence)
Example 5.92. A conditional expression
if (1+1 eq 2) then { "foo" : "yes" } else ()
Result (run with Zorba):
{ "foo" : "yes" }