Skip to content

Type Coercion

JSONata provides functions to convert values from one type to another. In DAQS rules, the most common conversions are: numbers to strings (for lookup keys), and raw parameter values to booleans (for shared boolean parameters).


$boolean() — convert a value to boolean

$boolean() converts any value to true or false using JSONata's truthiness rules:

Input Result
true true
false false
1 / any non-zero number true
0 false
"text" / any non-empty string true
"" false
null false
undefined false

Why this matters for shared parameters

Boolean shared parameters store their value as a number (0 or 1) in values.p_<guid>.value. Reading that value with .value gives 0 or 1, not true or false. Passing it through $boolean() makes it work correctly in conditions:

$sp := $lookup($object.values, "p_" & $guid);

{
  "exists":   $exists($sp),
  "hasValue": $exists($sp) ? $boolean($sp.hasValue) : false,
  "value":    $exists($sp) ? $boolean($sp.value) : null
}

Without $boolean(), a value of 0 is truthy in some JSONata contexts. With it, the semantics are explicit and reliable.


$number() — convert a value to a number

$number() converts a string or boolean to a number:

$number("42")      /* 42 */
$number("3.14")    /* 3.14 */
$number(true)      /* 1 */
$number(false)     /* 0 */
$number(null)      /* error — null cannot be converted */

Use $exists() before $number() when the input may be absent:

$exists(values.area) ? $number(values.area) : 0

$string() — convert to string

Covered in String Functions. The most frequent use is converting numeric IDs to string keys for $merge() and $lookup().


Common mistakes

  • Comparing $sp.value = true for a boolean parameter — the stored value is 1, not true. Use $boolean($sp.value) = true or simply $boolean($sp.value)
  • Calling $number(null) — throws an error. Always guard with $exists() first
  • Trusting raw hasValue as a boolean — it is a number; use $boolean($sp.hasValue) to be safe