pkg/yqlib/doc/operators/parent.md
Parent simply returns the parent nodes of the matching nodes.
Given a sample.yml file of:
a:
nested: cat
then
yq '.a.nested | parent' sample.yml
will output
nested: cat
Given a sample.yml file of:
a:
fruit: apple
name: bob
b:
fruit: banana
name: sam
then
yq '.. | select(. == "banana") | parent' sample.yml
will output
fruit: banana
name: sam
Given a sample.yml file of:
a:
fruit: apple
name: bob
b:
fruit: banana
name: sam
then
yq '.. | select(. == "banana") | parent.name' sample.yml
will output
sam
Match all parents
Given a sample.yml file of:
a:
b:
c: cat
then
yq '.a.b.c | parents' sample.yml
will output
- c: cat
- b:
c: cat
- a:
b:
c: cat
Use negative numbers to get the top parents. You can think of this as indexing into the 'parents' array above
Given a sample.yml file of:
a:
b:
c: cat
then
yq '.a.b.c | parent(-1)' sample.yml
will output
a:
b:
c: cat
Alias for parent(-1), returns the top level parent. This is usually the document node.
Given a sample.yml file of:
a:
b:
c: cat
then
yq '.a.b.c | root' sample.yml
will output
a:
b:
c: cat
You can optionally supply the number of levels to go up for the parent, the default being 1.
Given a sample.yml file of:
a:
b:
c: cat
then
yq '.a.b.c | parent(2)' sample.yml
will output
b:
c: cat
Given a sample.yml file of:
a:
b:
c: cat
then
yq '.a.b.c | parent(3)' sample.yml
will output
a:
b:
c: cat
Similarly, use negative numbers to index backwards from the parents array
Given a sample.yml file of:
a:
b:
c: cat
then
yq '.a.b.c | parent(-2)' sample.yml
will output
b:
c: cat
Given a sample.yml file of:
{}
then
yq 'parent' sample.yml
will output