y
y
yq
Search…
With
Use the with operator to conveniently make multiple updates to a deeply nested path, or to update array elements relatively to each other. The first argument expression sets the root context, and the second expression runs against that root context.

Update and style

Given a sample.yml file of:
1
a:
2
deeply:
3
nested: value
Copied!
then
1
yq eval 'with(.a.deeply.nested; . = "newValue" | . style="single")' sample.yml
Copied!
will output
1
a:
2
deeply:
3
nested: 'newValue'
Copied!

Update multiple deeply nested properties

Given a sample.yml file of:
1
a:
2
deeply:
3
nested: value
4
other: thing
Copied!
then
1
yq eval 'with(.a.deeply; .nested = "newValue" | .other= "newThing")' sample.yml
Copied!
will output
1
a:
2
deeply:
3
nested: newValue
4
other: newThing
Copied!

Update array elements relatively

The second expression runs with each element of the array as it's contextual root. This allows you to make updates relative to the element.
Given a sample.yml file of:
1
myArray:
2
- a: apple
3
- a: banana
Copied!
then
1
yq eval 'with(.myArray[]; .b = .a + " yum")' sample.yml
Copied!
will output
1
myArray:
2
- a: apple
3
b: apple yum
4
- a: banana
5
b: banana yum
Copied!
Last modified 2mo ago