jq 输出到非 json 列表以用于循环

jq output into a non json list to use for a loop

我确定有人问过这个问题,但我已经搜索过但找不到任何内容来回答我的问题...

我正在寻找一种简单的方法将 json 列表输出为非 json 列表以使用 while 循环进行处理。这意味着我需要去除引号、逗号和括号..我知道我可以用 cut 来做,但我确定我错过了一个更简单的方法..

假设我正在使用 aws 命令获取资源列表,然后我想在 while 循环中处理该列表...

aws eks list-clusters | jq .clusters | while read cluster; do something; done

类似的东西..我在这里是不是很愚蠢?

使用[]将列表变成一系列单独的字符串,并使用--raw-output / -r不带引号输出它们:

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