Parent
Parent simply returns the parent nodes of the matching nodes.
Simple example
Given a sample.yml file of:
a:
nested: catthen
yq '.a.nested | parent' sample.ymlwill output
nested: catParent of nested matches
Given a sample.yml file of:
a:
fruit: apple
name: bob
b:
fruit: banana
name: samthen
yq '.. | select(. == "banana") | parent' sample.ymlwill output
fruit: banana
name: samGet parent attribute
Given a sample.yml file of:
a:
fruit: apple
name: bob
b:
fruit: banana
name: samthen
yq '.. | select(. == "banana") | parent.name' sample.ymlwill output
samGet parents
Match all parents
Given a sample.yml file of:
a:
b:
c: catthen
yq '.a.b.c | parents' sample.ymlwill output
- c: cat
- b:
c: cat
- a:
b:
c: catN-th parent
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: catthen
yq '.a.b.c | parent(2)' sample.ymlwill output
b:
c: catN-th parent - another level
Given a sample.yml file of:
a:
b:
c: catthen
yq '.a.b.c | parent(3)' sample.ymlwill output
a:
b:
c: catNo parent
Given a sample.yml file of:
{}then
yq 'parent' sample.ymlwill output
Last updated
Was this helpful?