yamlyq

yq v4: print all key value pairs with full key path


I'm trying to determine the correct syntax for using yq to print all key/value pairs from a given yaml input using yq v4 - with the desired output having the full key "path". This was possible using v3 such as this:

$ cat << EOF | yq r -p pv - '**'
> a:
>   b: foo
>   c: bar
> EOF
a.b: foo
a.c: bar

but I'm having difficulty wrapping my head around the new syntax.

Any help is greatly appreciated.


Solution

  • $ cat << EOF | yq e '.. | select(. == "*") | {(path | join(".")): .} ' -
    > a:
    >   b: foo
    >   c: bar
    > EOF
    a.b: foo
    a.c: bar
    

    What does this do? Let's go over it:

    Edit: to get sequence indexes in square brackets ([0] etc), do

    $ cat << EOF | yq e '.. | select(. == "*") | . as $x | { ( [ path | ( .[] | select((. | tag) == "!!int") |= (["[", ., "]"] | join("")) ) ] | join(".") | sub(".\[", "[") ) : $x }' -
    

    This seems like there should be a simpler way to do it, but I don't know yq well enough to figure it out.