Product SiteDocumentation Site

8.7. Logic

JSONiq logics support is based on two-valued logics: there is just true and false and nothing else.
Non-boolean operands get automatically converted to either true or false, or an error is raised. The boolean() function performs a manual conversion. The rules for conversion were designed in such a way that it feels "natural". Here they are:
Example 8.12. Conversion to booleans.
{
  "empty-sequence" : boolean(()),
  "null" : boolean(null),
  "non-empty-string" : boolean("foo"),
  "empty-string" : boolean(""),
  "zero" : boolean(0),
  "not-zero" : boolean(1e42)
},
null and "foo"
Results:
{
  "empty-sequence" : false,
  "null" : false,
  "non-empty-string" : true,
  "empty-string" : false,
  "zero" : false,
  "not-zero" : true
}
false

8.7.1. Propositional Logic

JSONiq supports the most famous three boolean operations: conjunction, disjunction, and negation. Negation has the highest precedence, then conjunction, then disjunction. Comparisons have a higher precedence than all logical operations. Parentheses can override.
Example 8.13. Logics with booleans.
true and ( true or not true ),
1 + 1 eq 2 or not 1 + 1 eq 3
Results:
true
true

A sequence with more than one item, or singleton objects and arrays cannot be converted to a boolean. An error is raised if it is attempted.
Unlike in C++ or Java, you cannot rely on the order of evaluation of the operands of a boolean operation. The following query may return true or may raise an error.
Example 8.14. Non-determinism in presence of errors.
true or (1 div 0)
Results:
true