y
y
yq
Search…
How It Works
In yq expressions are made up of operators and pipes. A context of nodes is passed through the expression and each operation takes the context as input and returns a new context as output. That output is piped in as input for the next operation in the expression. To begin with, the context is set to the first yaml document of the first yaml file (if processing in sequence using eval).
Lets look at a couple of examples.

Simple assignment example

Given a document like:
1
a: cat
2
b: dog
Copied!
with an expression:
1
.a = .b
Copied!
Like math expression - operator precedence is important.
The = operator takes two arguments, a lhs expression, which in this case is .a and rhs expression which is .b.
It pipes the current, lets call it 'root' context through the lhs expression of .a to return the node
1
cat
Copied!
Sidenote: this node holds not only its value 'cat', but comments and metadata too, including path and parent information.
The = operator then pipes the 'root' context through the rhs expression of .b to return the node
1
dog
Copied!
Both sides have now been evaluated, so now the operator copies across the value from the RHS (.b) to the the LHS (.a), and it returns the now updated context:
1
a: dog
2
b: dog
Copied!

Complex assignment, operator precedence rules

Just like math expression - yq expression have an order of precedence. The pipe | operator has a low order of precedence, so operators with higher precedence will get evalated first.
Most of the time, this is intuitively what you'd want, for instance .a = "cat" | .b = "dog" is effectively: (.a = "cat") | (.b = "dog").
However, this is not always the case, particularly if you have a complex LHS or RHS expression, for instance if you want to select particular nodes to update.
Lets say you had:
1
- name: bob
2
fruit: apple
3
- name: sally
4
fruit: orange
Copied!
Lets say you wanted to update the sally entry to have fruit: 'mango'. The incorrect way to do that is: .[] | select(.name == "sally") | .fruit = "mango".
Becasue | has a low operator precedence, this will be evaluated (incorrectly) as : (.[]) | (select(.name == "sally")) | (.fruit = "mango"). What you'll see is only the updated segment returned:
1
name: sally
2
fruit: mango
Copied!
To properly update this yaml, you will need to use brackets (think BODMAS from maths) and wrap the entire LHS: (.[] | select(.name == "sally") | .fruit) = "mango"
Now that entire LHS expression is passed to the 'assign' (=) operator, and the yaml is correctly updated and returned:
1
- name: bob
2
fruit: apple
3
- name: sally
4
fruit: mango
Copied!

Relative update (e.g. |=)

There is another form of the = operator which we call the relative form. It's very similar to = but with one key difference when evaluating the RHS expression.
In the plain form, we pass in the 'root' level context to the RHS expression. In relative form, we pass in each result of the LHS to the RHS expression. Let's go through an example.
Given a document like:
1
a: 1
2
b: thing
Copied!
with an expression:
1
.a |= . + 1
Copied!
Similar to the = operator, |= takes two operands, the LHS and RHS.
It pipes the current context (the whole document) through the LHS expression of .a to get the node value:
1
1
Copied!
Now it pipes that LHS context into the RHS expression . + 1 (whereas in the = plain form it piped the original document context into the RHS) to yield:
1
2
Copied!
The assignment operator then copies across the value from the RHS to the value on the LHS, and it returns the now updated 'root' context:
1
a: 2
2
b: thing
Copied!
Last modified 25d ago