Search code examples
bashyq

How to get key and value with yq V4.x.x


We have upgraded yq from version 3.x.x to version 4.x.x and there is a part that does not work the same when getting a key value from the following structure:

key: a
value:
  enabled: true
  project: ../../a
  version: v1.1.1
  persistence: true
key: b
value:
  enabled: true
  project: ../../b
  version: v1.1.1
  persistence: true
key: c
value:
  enabled: true
  project: ../../c
  version: v1.1.1
  persistence: false

With the following yq command I would get the following

"a": true, "b": true, "c": false

This is the command

yq '.applications | to_entries | .[] | select(.value.enabled == true) .key, .value.persistence' manifest.yml 

and now the command returns this

a
b
c
null

I have recently started using yq and I really can't understand it.


Solution

  • There are two things going on here. First, I think the data structure you've presented in your question is erroneous; looking at the filter you're trying to run, I think the original data structure looks like this:

    applications:
      a:
        enabled: true
        project: ../../a
        version: v1.1.1
        persistence: true
      b:
        enabled: true
        project: ../../b
        version: v1.1.1
        persistence: true
      c:
        enabled: true
        project: ../../c
        version: v1.1.1
        persistence: false
    

    If we apply your filter using kislyuk/yq, then we see the following output:

    "a"
    true
    "b"
    true
    "c"
    false
    

    On the other hand, we if use mikefarah/yq, we get:

    a
    b
    c
    null
    

    This is because these two commands are different. The first is a simple Python wrapper for the jq command, whereas the second is a standalone Go binary. They support different syntax.

    You cannot "upgrade" from the kislyuk version (latest version: 3.1.0) to the mikefarah version (latest version: 4.30.8).