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 select entry value based on subkey

Using yq (v4.25.3), and considering the following yaml file

accounts:

- account_id: 'XXXXXXXX'
  name: sandbox
  deploy_iam: true
  role: arn:aws:iam::XXXXXXXX:role/iam_role
  regions:
  - all

- account_id: 'YYYYYYY'
  name: dev
  deploy_iam: true
  role: arn:aws:iam::YYYYYYY:role/iam_role
  regions:
  - all

Is it possible to get the value of the deploy_iam attribute given an account_id value?

I can get the list of account_id with
yq '.accounts[].account_id' < accounts.yml

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

And I tried to filter using with_entries

 yq '.accounts[].account_id |= with_entries(select(.key == "XXXXXXXX"))' < accounts.yml

Without luck so far.

Any idea?

>Solution :

With mikfarah/yq it should be pretty straightforward. Select the required object with the select() expression and access the required field with the dot notation

yq '.accounts[] | select(.account_id == "XXXXXXXX").deploy_iam' < accounts.yml
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