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

前端 未结 5 1516
眼角桃花
眼角桃花 2021-02-02 07:48

How can I run a Bash command for every JSON object in a JSON array using jq? So far I have this:

cat credentials.json | jq -r \'.[] | .user, .date, .         


        
5条回答
  •  逝去的感伤
    2021-02-02 08:23

    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.

提交回复
热议问题