Skip to main content
added 87 characters in body
Source Link
glenn jackman
  • 88.5k
  • 16
  • 124
  • 179

Do it in jq, but see @Kusalananda's answer first

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

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

Do it in jq, but see @Kusalananda's answer first

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
Post Undeleted by glenn jackman
added 263 characters in body
Source Link
glenn jackman
  • 88.5k
  • 16
  • 124
  • 179

Do it in jq

jq -r '.host_components[].HostRoles.host_name | join(",")'

AhNo, that's wrong. This is what you may need the map function to create a list on the left-hand side:

jq -r 'map'.host_components | map(.host_components[]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

Do it in jq

jq -r '.host_components[].HostRoles.host_name | join(",")'

Ah, you may need the map function to create a list on the left-hand side:

jq -r 'map(.host_components[].HostRoles.host_name) | join(",")'

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
Post Deleted by glenn jackman
added 158 characters in body
Source Link
glenn jackman
  • 88.5k
  • 16
  • 124
  • 179

Do it in jq

jq -r '.host_components[].HostRoles.host_name | join(",")'

Ah, you may need the map function to create a list on the left-hand side:

jq -r 'map(.host_components[].HostRoles.host_name) | join(",")'

Do it in jq

jq -r '.host_components[].HostRoles.host_name | join(",")'

Do it in jq

jq -r '.host_components[].HostRoles.host_name | join(",")'

Ah, you may need the map function to create a list on the left-hand side:

jq -r 'map(.host_components[].HostRoles.host_name) | join(",")'
Source Link
glenn jackman
  • 88.5k
  • 16
  • 124
  • 179
Loading