Conditionals
Conditionals allow you to select different values depending on an expression.
Dasel v3 supports both a long form if/else block and a compact ternary operator.
Long Form (if/else)
The long form is more explicit and easier to read for complex conditions.
if (<condition>) { <then-expression> } else { <else-expression> }
<condition>must evaluate to a boolean.<then-expression>is evaluated if the condition is true.<else-expression>is evaluated if the condition is false.
Example
Input JSON
{
"foo": {
"bar": "baz",
"bong": "selected",
"qux": "not-selected"
}
}
Query
$ dasel -i json -f input.json '.foo.if(bar == "baz") { bong } else { qux }'
Output
selected
Ternary Operator (? :)
Warning
Not yet implemented.
The ternary form is shorter and useful for inline conditions.
<condition> ? <then-expression> : <else-expression>
Example
Input JSON
{
"foo": {
"bar": "qux",
"bong": "selected",
"qux": "not-selected"
}
}
Query
$ dasel -i json -f input.json '.foo.(bar == "baz" ? bong : qux)'
Output
not-selected
Literals and Nesting
Both forms support literals and nested expressions.
Example 1: Literal results
Input JSON
{ "count": 7 }
Query
$ dasel -i json -f input.json 'if(count > 5) { "many" } else { "few" }'
Output
many
Example 2: Nested ternaries
Input JSON
{ "foo": { "bar": "zap", "bong": "BONG", "qux": "QUX", "zap": "ZAP", "default": "DEF" } }
Query
$ dasel -i json -f input.json '.foo.(bar == "baz" ? bong : (bar == "qux" ? qux : default))'
Output
DEF
Notes
- An
elsebranch is required.
Both theif/elseand ternary forms must specify anelseresult. - Both branches must return a value.
A conditional always evaluates to a result — you cannot have an empty branch. - Parentheses are recommended when nesting conditionals.
- Both branches must be valid dasel expressions (selectors, literals, or functions).