Conditionals
Conditionals allow you to select different values depending on an expression. Dasel v3 supports an if/elseif/else block syntax and a ternary operator (?:).
Syntax
if (<condition>) { <then> } else { <else> }<condition>must evaluate to a boolean.<then>is evaluated if the condition is true.<else>is evaluated if the condition is false.An
elsebranch is always required.
Basic Example
Input JSON
{
"foo": {
"bar": "baz",
"bong": "selected",
"qux": "not-selected"
}
}Query
Output
Elseif Chains
Use elseif to chain multiple conditions. You can use as many elseif branches as needed.
Example
Input JSON
Query
Output
Fizzbuzz with elseif
Given numbers.json:
Literal Results
Both branches can return literal values, not just field lookups.
Input JSON
Query
Output
Nested Conditionals
Conditionals can be nested in the else branch.
For multi-branch cases, elseif is cleaner than nesting.
Ternary Operator
The ternary operator provides a compact inline syntax for conditionals.
Syntax
<condition>must evaluate to a boolean.<then>is returned when the condition is true.<else>is returned when the condition is false.
Example
Input JSON
Query
Output
Nested Ternary
Parentheses can be used to nest ternary expressions.
Output
Notes
An
elsebranch is always required — bothif/elseandif/elseif/elsemust have a finalelse.Both branches must return a value — you cannot have an empty branch.
Use
elseif(one word, no space) for chained conditions.Parentheses around the condition are required for
if/elseifblocks but not for the ternary operator.The ternary operator always requires both
?and:parts.
Last updated