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

Using yq to modify yml files

I am trying to add new arrays to the existing yml file

for example the test.yml looks like this

visibility:
  subscribe:
    enabled: true
    type: authenticated
  view:
    enabled: true
    type: public

and I am trying to modify it to look like this

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

visibility:
  subscribe:
    tags: []
    orgs: []
    enabled: true
    type: authenticated
  view:
    tags: []
    orgs: []
    enabled: true
    type: public

I have used yq to do that but there seems to be an issue it does not add any new field
Here is the bash script I made

yq '.visibility.subscribe |= {"orgs":"[]"} + .' test.yml
yq '.visibility.subscribe |= {"tags":"[]"} + .' test.yml
yq '.visibility.view |= {"orgs":"[]"} + .' test.yml
yq '.visibility.view |= {"tags":"[]"} + .' test.yml

Am I using the right logic ?

>Solution :

Presuming that you’re using the yq tool that wraps jq (which appears to be the case from your starting syntax):

yq '
  .visibility.subscribe.orgs = []
| .visibility.subscribe.tags = []
| .visibility.view.orgs = []
| .visibility.view.tags = []
' test.yml

…is a simpler way to achieve output that’s semantically identical (not necessarily syntactically identical!) to your stated/desired output. The most important change necessary was changing "[]" to [].

Note that the new document is written to stdout — test.yml is not modified in-place unless the -i argument is used.

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