1

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.

Andy Edgar
  • 86
  • 1
  • 5

1 Answers1

6
$ 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:

  • .. recursively select all values
  • select(. == "*") filter for scalar values (i.e. filter out the value of a)
  • (path | join(".")) gets the path as array and joins the elements with .
  • {…: .} create a mapping, having the joined paths as keys and their values as values

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

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

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

flyx
  • 35,506
  • 7
  • 89
  • 126
  • Hi flyx, Thank you for this.. if I have an list of values, it yields me something like this - a.b.0.name: testname a.b.0.state: teststate ......is it possible to get it out in standard yaml query path like this - a.b[0].name: testname a.b[0].state: teststate – Ponson Thankavel Apr 27 '22 at 15:31
  • 1
    @PonsonThankavel I updated the answer with code that does what you want. btw, you'll find no mention of „yaml query path“ in the YAML spec. I *think* this form is mainly used in conjunction with Spring boot, where this is the way to transform YAML into Java properties, but there's nothing *standard* about it. – flyx Apr 27 '22 at 16:08
  • Thank you very much... The output now has the array element notation now but it still contains a dot (.) character before the array index (like a.b.[0].state). is it possible to get rid of that dot character (like a.b[0].state).. Tried to tweak your expressions but couldn't figure this out... – Ponson Thankavel Apr 28 '22 at 07:23
  • tweaked the command like this and made it work along with sed... $ cat << EOF | yq e '.. | select(. == "*") | {(path | . as $x | (.[] | select((. | tag) == "!!int") |= (["||||[", ., "]"] | join(""))) | $x | join(".")): .} ' - | sed -e 's/.||||\\[/\\[/g' ..... Thank you flyx... you are awesome... – Ponson Thankavel Apr 28 '22 at 07:50
  • 1
    @PonsonThankavel Ah yes, I missed the dot. I updated the command to replace any `.[` with `[`, that should do it. Basically what your `sed` command does. – flyx Apr 28 '22 at 09:16
  • works very well... Thank you again flyx.... – Ponson Thankavel Apr 28 '22 at 12:34
  • Tried it with the most recent mikefarah.gitbook.io/ - it works flawlessly. – Igor Mar 15 '23 at 12:08