Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

yq – make key-value pairs from nested yaml structure

Having the following file structure:

A1:
    B1:
        description: "B1 field"
    B2:
        any-field: 1
        description: "B2 field"
A2:
    B3:
        any-field: 1
    B4:
        any-field: 1
        description: "B4 field"

I would like to get the following result:

B1: "B1 field"
B2: "B2 field"
B4: "B4 field"

So basically I need to filter out object without desired property (B3 in this case) and then map each from .*.* | keys to .*.*.description, but I failed to do it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

yq I am using is the newest (4.27.5) from https://github.com/mikefarah/yq

>Solution :

To go 2-levels deep, you can use the to_entries function twice, and then select the key name and the value containing the .description field

yq 'to_entries | .[].value | to_entries | 
      map(select(.value | has("description"))) | .[] | {.key: .value.description}' yaml
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading