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

jq if value contains then append

This might not be the ideal way to approach this but I am working on bulk update of some Grafana dashboards. When the expr key contains value that includes something like "sum((rate" I want to append to the value another string. Is this even possible?

 if (.dashboard.panels[].targets[].expr | contains("sum((rate")) then . += "TEST" end'

I’ve tried a few variations of then action and even removed the concatanation to see if I could get simple replace. But getting

jq: error: syntax error, unexpected end (Unix shell quoting issues?) at <top-level>, line 1:

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

>Solution :

You should then be able to update-assign part of your objects:

.dashboard.panels[].targets[].expr |= if contains("sum((rate") then . + "TEST" else . end

It’s also possible to do without the if conditional by first selecting all the interesting paths and then modifying only them:

(.dashboard.panels[].targets[].expr | select(contains("sum((rate"))) += "TEST"
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