Parent

Parent simply returns the parent nodes of the matching nodes.

Simple example

Given a sample.yml file of:

a:
  nested: cat

then

yq '.a.nested | parent' sample.yml

will output

nested: cat

Parent of nested matches

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

Get parent attribute

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

N-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: cat

then

yq '.a.b.c | parent(2)' sample.yml

will output

b:
  c: cat

N-th parent - another level

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

No parent

Given a sample.yml file of:

{}

then

yq 'parent' sample.yml

will output

Last updated