Using jq with bash to run command for each object in array

With xargs:

curl localhost:8082/connectors | jq .[] | xargs -L1 -I'{}' curl -XDELETE 'localhost:8082/connectors/{}' 

Or equivalently, to show the output of that first curl:

echo '["quickstart-file-sink4","quickstart-file-source","quickstart-file-sink","quickstart-file-sink2","quickstart-file-sink3","quickstart-file-source2"]' | jq .[] | xargs -L1 -I'{}' curl -XDELETE 'localhost:8082/connectors/{}' 

jq .[] strips off one level of containment, so that a list becomes output as one line per item.

xargs -L1 processes one line at a time

xargs -I'{}' specifies that the string {} be replaced with the input line when invoking the following command.

xargs is essentially a map operator for the shell.


Your best bet is probably to output each record in something like TSV format, then read that from a shell loop.

jq -r '.[]|[.user, .date, .email] | @tsv' |
  while IFS=$'\t' read -r user date email; do
    mycommand -u "$user" -d "$date" -e "$email"
  done

jq itself doesn't have anything like a system call to run an external command from within a filter, although it seems that they are working on it.

Tags:

Bash

Jq