jq output into a non json list to use for a loop
I'm sure this has been asked, but I've searched and can't find anything to answer my question...
I'm looking for a simple way to output a json list as a non json list to process using a while loop.. Which means I need to strip the quotes, commas and brackets.. I know I could do it with cut
but I'm sure I'm missing an easier way..
Say I'm using an aws command to get a list of resources and then I want to process that list in a while loop...
aws eks list-clusters | jq .clusters | while read cluster; do something; done
Something along these lines.. am I being stupid here?
Solution 1:
Use []
to turn the list into a series of individual strings, and --raw-output
/ -r
to output them without quotes:
With this option, if the filter's result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes. This can be useful for making jq filters talk to non-JSON-based systems.
aws eks list-clusters | jq -r '.clusters[]' | while read cluster; do something; done