I am trying to use jq to join the values inside a JSON array into a single line comma separated list. (Without trailing comma)

  "hardware": [

To create "abc, def, ghi"

I can join values together using

jq -jr '(.hardware[])' input.json


I have tried to insert comma and space but cannot work

jq -jr '(.hardware[]|join(", ")' 

jq: error: syntax error, unexpected $end (Unix shell quoting issues?) at <top-level>, line 1:

(.hardware[]|join(", ")

Could someone point me to the correct syntax to use?




You are looking for

jq -r '.hardware | join(", ")'

The syntax error from the version you posted is because the opening ( doesn't have a matching ), but in any case join needs to be given all the values at once, so .hardware is better than .hardware[] (which will pass them through one at a time).


Your jq expression contains unbalanced parentheses. The join() function also needs an array, not individual values (use .hardware | join(", ") rather than .hardware[] | join(", ")).

If you want properly formatted CSV output, you could use the expression .hardware | @csv:

$ jq -r '.hardware | @csv' file.json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.