Dynamic

Description

Dynamic selectors allow you to select items from lists or maps when you don't know the key or index. You can think of this as searching the current node.

Usage

You can use a selector as the key to create more complex conditions.
1
.(<key>=<value>)
Copied!

Key

The key defines which property/selector we should use to extract a value.
If <key> is:
    . or value - dasel checks if the current nodes value is <value>.
    - or keyValue - dasel checks if the key/name/index of the current node is <value>.
    Else dasel uses the key as a selector itself and compares the result against <value>.

Value

The value is the expected value for the check to pass.
Note that dasel will stringy values prior to checking if they match.

Comparisons

Dasel supports the following comparison operators:
Operator
Supported since
=
v0.0.4
>=
v1.14.0
>
v1.14.0
<
v1.14.0
<=
v1.14.0
!=
v1.17.0

Multiple conditions

You can use multiple dynamic selectors within the same part to perform AND logic.
1
.(<key>=<value>)(<key2>=<value2>)
Copied!

Non-object values

You can evaluate and check against non-object values by defining the key as either value or .. Doing so will tell dasel extract the value of the current node and compare that against <value>.

Selectors as a key

When performing dynamic checks dasel internally creates a new root node at the current position and queries data from there.
This means that you can use fully formed selectors as a key to create more advanced logic.
1
.users.(.addresses.(.primary=true).number=123).name.first
Copied!
The above selector in plain English may read as...
Give me the first name of the user who's primary address is at number 123.
The resolution of that query looks something like this:
1
.users.(.addresses.(.primary=true).number=123).name.first
2
.users.(.addresses.[0].number=123).name.first
3
.users.[0].name.first
Copied!

Example

simple_input.yaml
1
colourCodes:
2
- name: red
3
rgb: ff0000
4
- name: green
5
rgb: 00ff00
6
- name: blue
7
rgb: 0000ff
Copied!
advanced_input.json
1
{
2
"users": [
3
{
4
"name": {
5
"first": "Tom",
6
"last": "Wright"
7
},
8
"addresses": [
9
{
10
"primary": true,
11
"number": 123
12
},
13
{
14
"primary": false,
15
"number": 456
16
}
17
]
18
}
19
]
20
}
Copied!

Single condition

1
dasel select -f simple_input.yaml '.colourCodes.(name=red).rgb'
2
​
3
ff0000
Copied!

Multiple conditions

1
dasel select -f simple_input.yaml '.colourCodes.(name=red)(rgb=ff0000).rgb'
2
​
3
ff0000
Copied!

Selector as a key

1
dasel -f advanced_input.json '.users.(.addresses.(.primary=true).number=123).name.first'
2
​
3
"Tom"
Copied!
Last modified 2mo ago