Conditional expressions

JSONiq follows the W3C standard for conditional expressions. The following explanations, provided as an informal summary for convenience, are non-normative.

Figure 49. IfExpr

IfExpr

A conditional expressions allows you to pick one or another value depending on a boolean value.

Example 97. 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 98. A conditional expression

if (null) then { "foo" : "yes" } else { "foo" : "no" }
      

Result (run with Zorba): { "foo" : "no" }


Example 99. A conditional expression

if (1) then { "foo" : "yes" } else { "foo" : "no" }
      

Result (run with Zorba): { "foo" : "yes" }


Example 100. A conditional expression

if (0) then { "foo" : "yes" } else { "foo" : "no" }
        

Result (run with Zorba): { "foo" : "no" }


Example 101. A conditional expression

if ("foo") then { "foo" : "yes" } else { "foo" : "no" }
      

Result (run with Zorba): { "foo" : "yes" }


Example 102. A conditional expression

if ("") then { "foo" : "yes" } else { "foo" : "no" }
        

Result (run with Zorba): { "foo" : "no" }


Example 103. A conditional expression

if (()) then { "foo" : "yes" } else { "foo" : "no" }
        

Result (run with Zorba): { "foo" : "no" }


Example 104. 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 105. A conditional expression

if (1+1 eq 2) then { "foo" : "yes" } else ()
        

Result (run with Zorba): { "foo" : "yes" }