Skip to main content
added 127 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

If what you want is CSV-formatted output from your JSON document, then you may use the @csv operator in jq.

somecommand | jq -r '[ .host_components[].HostRoles.host_name ] | @csv'

This uses the same expression that you're using in the question to pull out the data that you want, but it puts all the host names into an array. The array is then passed through @csv which makes sure that that the data is properly quoted for CSV output.

You would expect to get the following output from this:

"zk0-mycluster.net","zk1-mycluster.net","zk2-mycluster.net"

Any commas, double quotes, or newlines embedded in the values would be properly quoted as expected in a CSV-formatted file.

If what you want is CSV-formatted output from your JSON document, then you may use the @csv operator in jq.

somecommand | jq -r '[ .host_components[].HostRoles.host_name ] | @csv'

This uses the same expression that you're using in the question to pull out the data that you want, but it puts all the host names into an array. The array is then passed through @csv which makes sure that that the data is properly quoted for CSV output.

You would expect to get the following output from this:

"zk0-mycluster.net","zk1-mycluster.net","zk2-mycluster.net"

If what you want is CSV-formatted output from your JSON document, then you may use the @csv operator in jq.

somecommand | jq -r '[ .host_components[].HostRoles.host_name ] | @csv'

This uses the same expression that you're using in the question to pull out the data that you want, but it puts all the host names into an array. The array is then passed through @csv which makes sure that that the data is properly quoted for CSV output.

You would expect to get the following output from this:

"zk0-mycluster.net","zk1-mycluster.net","zk2-mycluster.net"

Any commas, double quotes, or newlines embedded in the values would be properly quoted as expected in a CSV-formatted file.

Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

If what you want is CSV-formatted output from your JSON document, then you may use the @csv operator in jq.

somecommand | jq -r '[ .host_components[].HostRoles.host_name ] | @csv'

This uses the same expression that you're using in the question to pull out the data that you want, but it puts all the host names into an array. The array is then passed through @csv which makes sure that that the data is properly quoted for CSV output.

You would expect to get the following output from this:

"zk0-mycluster.net","zk1-mycluster.net","zk2-mycluster.net"