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)
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
selectedTernary Operator (? :)
? :)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-selectedLiterals 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
manyExample 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
DEFNotes
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).
Last updated