Merge jq output into a comma separated string
Do it in jq
jq -r '.host_components[].HostRoles.host_name | join(",")'
No, that's wrong. This is what you need:
jq -r '.host_components | map(.HostRoles.host_name) | join(",")'
Demo:
jq -r '.host_components | map(.HostRoles.host_name) | join(",")' <<DATA
{"host_components":[
{"HostRoles":{"host_name":"one"}},
{"HostRoles":{"host_name":"two"}},
{"HostRoles":{"host_name":"three"}}
]}
DATA
outputs
one,two,three
paste
is the best tool to do this job:
your_command | paste -sd, -
If you want to use awk
, just print with no newline
:
<your command> | awk 'NR > 1 { printf(",") } {printf "%s",$0}'