Variables
Dasel allows you to define variables for use in your queries.
Variables are referenced using a $ prefix.
Note that these variables are essentially globals, and once defined, they are accessible at any point in the execution.
Variables can be used alongside stdin.
Inline
Inline variables are those defined within a dasel query itself and should be terminated with a semicolon.
$ dasel -i json '$x = 1; $y = 2; $x + $y'
3Multi-step data transformation
$ echo '{"users": [{"name": "Alice"}, {"name": "Bob"}]}' \
| dasel -i json '
$names = users.map(name);
$count = len($names);
"Found " + toString($count) + " users"
'
"Found 2 users"From the environment
You can access environment variables using $ENV_VAR_NAME.
Note that changes to environment variables within dasel are not supported.
From the CLI
You can set variables from the CLI by passing additional arguments in the form of:
--var name=format:content
If you wish to pass a file as a variable you can use:
--var name=format:file:filepath
Note that at this time variables from the CLI are currently required to be documents read from the file system. There are plans to change this in the future.
Standard variables
Some variables are provided by dasel and will always exist:
$root- The root document passed throughstdinor-f.
$this
$this refers to the current element being processed. It is available inside expressions that iterate over arrays:
map—$thisis the current element being transformed.filter—$thisis the current element being tested.each—$thisis the current element being visited.sortBy—$thisis the current element being compared.
It is also available in spread contexts and when accessing the current node in conditionals.
Examples
$key
$key refers to the current index or key during iteration. For arrays/slices it is an integer index (starting at 0); for maps/objects it is the string key name.
$key is available inside all iteration expressions:
mapValues—$keyis the map key name (string).search/ recursive descent —$keyis the map key (string) or slice index (int) depending on context.
$key is scoped to the iteration — it does not leak into outer expressions.
Examples
Last updated