Search code examples
jsonbashiterationexport-to-csvjq

get json output with jq comma separated duplicate my output


I'm trying to get 2 json registers of a query in same line comma separated but allways duplicate me the output. Could you help me please? Thanks

bash-4.2$ rm run works:status::get -s "ctm=SERVERA&work=*&status=Failed" | $PATH_API/jq -r '.statuses[].name, .statuses[].jobId  // empty' --raw-output
JOBA
JOBA
SERVERA:0rhh0
SERVERA:0rhgz

bash-4.2$
bash-4.2$ rm run works:status::get -s "ctm=SERVERA&work=*&status=Failed" | $PATH_API/jq -r '"\(.statuses[].name), \(.statuses[].jobId)"'  --raw-output
JOBA, SERVERA:0rhh0
JOBA, SERVERA:0rhh0
JOBA, SERVERA:0rhgz
JOBA, SERVERA:0rhgz

Solution

  • You should only enumerate once:

    .statuses[] | "\(.name), \(.jobId)"
    

    Incidentally, you might wish to consider using @csv instead of interpolation.