githubEdit

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'
3

Multi-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 through stdin or -f.

$this

$this refers to the current element being processed. It is available inside expressions that iterate over arrays:

  • map$this is the current element being transformed.

  • filter$this is the current element being tested.

  • each$this is the current element being visited.

  • sortBy$this is the current element being compared.

  • any / all / count$this is the current element being evaluated.

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:

$key is scoped to the iteration — it does not leak into outer expressions.

Examples

Last updated