简体   繁体   中英

Collecting results into a map from a yaml file with yq, values are unexpectedly null

I am trying to get the results of the following structure from a yaml file

applications:
  a:
    enabled: true
    persistence: true
  b:
    enabled: true
    persistence: true
  c:
    enabled: true
    persistence: false

Using yq by mikefarah v4.30.4:

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

But I get this output:

[{a: null}, {b: null}, {c: null}]

Trying other examples I get the following result

yq '.applications | to_entries | .[] |select(.value.enabled == true) | {.key: {.persistence: .value.persistence}}' manifest.yml
a:
  null: true
b:
  null: true
c:
  null: false

The result should be

a: true, b: true, c: false

The most immediate problem could be solved by more parens ( (.key: .value.persistence) instead of .key: .value.persistence ), but to get a single map as output, you should use a reducer:

yq '
(.applications
| to_entries
| .[]
| select(.value.enabled == true)
) as $i ireduce({}; .[$i.key] = $i.value.persistence)
' manifest.yml

...yields as output:

a: true
b: true
c: false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM