2021-11-03 04:00:58 +00:00
|
|
|
# Traverse (Read)
|
|
|
|
|
2023-03-16 02:39:36 +00:00
|
|
|
This is the simplest (and perhaps most used) operator. It is used to navigate deeply into yaml structures.
|
2021-11-03 04:00:58 +00:00
|
|
|
|
|
|
|
## Simple map navigation
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
{a: {b: apple}}
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.a' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
{b: apple}
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Splat
|
|
|
|
Often used to pipe children into other operators
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
[{b: apple}, {c: banana}]
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
{b: apple}
|
|
|
|
{c: banana}
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Optional Splat
|
|
|
|
Just like splat, but won't error if you run it against scalars
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
"cat"
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
```
|
|
|
|
|
|
|
|
## Special characters
|
2023-03-16 02:39:36 +00:00
|
|
|
Use quotes with square brackets around path elements with special characters
|
2021-11-03 04:00:58 +00:00
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
{"{}": frog}
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.["{}"]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
frog
|
|
|
|
```
|
|
|
|
|
|
|
|
## Nested special characters
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
"key.withdots":
|
|
|
|
"another.key": apple
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.a["key.withdots"]["another.key"]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
apple
|
|
|
|
```
|
|
|
|
|
|
|
|
## Keys with spaces
|
2023-03-16 02:39:36 +00:00
|
|
|
Use quotes with square brackets around path elements with special characters
|
2021-11-03 04:00:58 +00:00
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
{"red rabbit": frog}
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.["red rabbit"]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
frog
|
|
|
|
```
|
|
|
|
|
|
|
|
## Dynamic keys
|
|
|
|
Expressions within [] can be used to dynamically lookup / calculate keys
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
{b: apple, apple: crispy yum, banana: soft yum}
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[.b]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
crispy yum
|
|
|
|
```
|
|
|
|
|
|
|
|
## Children don't exist
|
|
|
|
Nodes are added dynamically while traversing
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
{c: banana}
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.a.b' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
null
|
|
|
|
```
|
|
|
|
|
|
|
|
## Optional identifier
|
|
|
|
Like jq, does not output an error when the yaml is not an array or object as expected
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
[1, 2, 3]
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.a?' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
```
|
|
|
|
|
|
|
|
## Wildcard matching
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
{a: {cat: apple, mad: things}}
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.a."*a*"' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
apple
|
|
|
|
things
|
|
|
|
```
|
|
|
|
|
|
|
|
## Aliases
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
{a: &cat {c: frog}, b: *cat}
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.b' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
*cat
|
|
|
|
```
|
|
|
|
|
|
|
|
## Traversing aliases with splat
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
{a: &cat {c: frog}, b: *cat}
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.b[]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
frog
|
|
|
|
```
|
|
|
|
|
|
|
|
## Traversing aliases explicitly
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
{a: &cat {c: frog}, b: *cat}
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.b.c' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
frog
|
|
|
|
```
|
|
|
|
|
|
|
|
## Traversing arrays by index
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
[1, 2, 3]
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[0]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
1
|
|
|
|
```
|
|
|
|
|
|
|
|
## Traversing nested arrays by index
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
[[], [cat]]
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[1][0]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
cat
|
|
|
|
```
|
|
|
|
|
|
|
|
## Maps with numeric keys
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
{2: cat}
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[2]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
cat
|
|
|
|
```
|
|
|
|
|
|
|
|
## Maps with non existing numeric keys
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
{a: b}
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[0]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
null
|
|
|
|
```
|
|
|
|
|
|
|
|
## Traversing merge anchors
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
foo: &foo
|
|
|
|
a: foo_a
|
|
|
|
thing: foo_thing
|
|
|
|
c: foo_c
|
|
|
|
bar: &bar
|
|
|
|
b: bar_b
|
|
|
|
thing: bar_thing
|
|
|
|
c: bar_c
|
|
|
|
foobarList:
|
|
|
|
b: foobarList_b
|
|
|
|
!!merge <<:
|
|
|
|
- *foo
|
|
|
|
- *bar
|
|
|
|
c: foobarList_c
|
|
|
|
foobar:
|
|
|
|
c: foobar_c
|
|
|
|
!!merge <<: *foo
|
|
|
|
thing: foobar_thing
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.foobar.a' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
foo_a
|
|
|
|
```
|
|
|
|
|
|
|
|
## Traversing merge anchors with override
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
foo: &foo
|
|
|
|
a: foo_a
|
|
|
|
thing: foo_thing
|
|
|
|
c: foo_c
|
|
|
|
bar: &bar
|
|
|
|
b: bar_b
|
|
|
|
thing: bar_thing
|
|
|
|
c: bar_c
|
|
|
|
foobarList:
|
|
|
|
b: foobarList_b
|
|
|
|
!!merge <<:
|
|
|
|
- *foo
|
|
|
|
- *bar
|
|
|
|
c: foobarList_c
|
|
|
|
foobar:
|
|
|
|
c: foobar_c
|
|
|
|
!!merge <<: *foo
|
|
|
|
thing: foobar_thing
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.foobar.c' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
foo_c
|
|
|
|
```
|
|
|
|
|
|
|
|
## Traversing merge anchors with local override
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
foo: &foo
|
|
|
|
a: foo_a
|
|
|
|
thing: foo_thing
|
|
|
|
c: foo_c
|
|
|
|
bar: &bar
|
|
|
|
b: bar_b
|
|
|
|
thing: bar_thing
|
|
|
|
c: bar_c
|
|
|
|
foobarList:
|
|
|
|
b: foobarList_b
|
|
|
|
!!merge <<:
|
|
|
|
- *foo
|
|
|
|
- *bar
|
|
|
|
c: foobarList_c
|
|
|
|
foobar:
|
|
|
|
c: foobar_c
|
|
|
|
!!merge <<: *foo
|
|
|
|
thing: foobar_thing
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.foobar.thing' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
foobar_thing
|
|
|
|
```
|
|
|
|
|
|
|
|
## Splatting merge anchors
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
foo: &foo
|
|
|
|
a: foo_a
|
|
|
|
thing: foo_thing
|
|
|
|
c: foo_c
|
|
|
|
bar: &bar
|
|
|
|
b: bar_b
|
|
|
|
thing: bar_thing
|
|
|
|
c: bar_c
|
|
|
|
foobarList:
|
|
|
|
b: foobarList_b
|
|
|
|
!!merge <<:
|
|
|
|
- *foo
|
|
|
|
- *bar
|
|
|
|
c: foobarList_c
|
|
|
|
foobar:
|
|
|
|
c: foobar_c
|
|
|
|
!!merge <<: *foo
|
|
|
|
thing: foobar_thing
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.foobar[]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
foo_c
|
|
|
|
foo_a
|
|
|
|
foobar_thing
|
|
|
|
```
|
|
|
|
|
|
|
|
## Traversing merge anchor lists
|
|
|
|
Note that the later merge anchors override previous
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
foo: &foo
|
|
|
|
a: foo_a
|
|
|
|
thing: foo_thing
|
|
|
|
c: foo_c
|
|
|
|
bar: &bar
|
|
|
|
b: bar_b
|
|
|
|
thing: bar_thing
|
|
|
|
c: bar_c
|
|
|
|
foobarList:
|
|
|
|
b: foobarList_b
|
|
|
|
!!merge <<:
|
|
|
|
- *foo
|
|
|
|
- *bar
|
|
|
|
c: foobarList_c
|
|
|
|
foobar:
|
|
|
|
c: foobar_c
|
|
|
|
!!merge <<: *foo
|
|
|
|
thing: foobar_thing
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.foobarList.thing' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
bar_thing
|
|
|
|
```
|
|
|
|
|
|
|
|
## Splatting merge anchor lists
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
foo: &foo
|
|
|
|
a: foo_a
|
|
|
|
thing: foo_thing
|
|
|
|
c: foo_c
|
|
|
|
bar: &bar
|
|
|
|
b: bar_b
|
|
|
|
thing: bar_thing
|
|
|
|
c: bar_c
|
|
|
|
foobarList:
|
|
|
|
b: foobarList_b
|
|
|
|
!!merge <<:
|
|
|
|
- *foo
|
|
|
|
- *bar
|
|
|
|
c: foobarList_c
|
|
|
|
foobar:
|
|
|
|
c: foobar_c
|
|
|
|
!!merge <<: *foo
|
|
|
|
thing: foobar_thing
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.foobarList[]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
bar_b
|
|
|
|
foo_a
|
|
|
|
bar_thing
|
|
|
|
foobarList_c
|
|
|
|
```
|
|
|
|
|
|
|
|
## Select multiple indices
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 04:13:18 +00:00
|
|
|
{a: [a, b, c]}
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.a[0, 2]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a
|
|
|
|
c
|
|
|
|
```
|
|
|
|
|