Skip to main content
added 207 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

.[].results[] is a set of arrays. In each array, the first element is what you want to have in the first column, and the second element is another array that we want to loop over.

So let's keep track of the first element in $name (assuming this is a cluster name of some sort), and then output this together with each element of the sub-array:

.[].results[] | .[0] as $name | .[1][]? // .[1] | [ $name, . ] | @csv

The bit that says .[1][]? // .[1] selects the elements of the sub-array if it exists, otherwise it selects the second element of the array (and assumes that it's a scalar instead).

On the command line:

jq -r '.[].results[] | .[0] as $name | .[1][]? // .[1] | [ $name, . ] | @csv' file

Result, given your example document:

"sm-clust001","163slesm02"
"sm-clust001","163slesm01"
"sm-cssl112","ucsbnchac240"
"sm-cssl112","ucsbnchac209"
"sm-cssl112","ucsbnchac241"
"sm-cssl112","ucsbnchac242"
"ASite","unixhost1123"

This solution is generalized for any number of columns in my answer to the user's followup question.

.[].results[] is a set of arrays. In each array, the first element is what you want to have in the first column, and the second element is another array that we want to loop over.

So let's keep track of the first element in $name (assuming this is a cluster name of some sort), and then output this together with each element of the sub-array:

.[].results[] | .[0] as $name | .[1][]? // .[1] | [ $name, . ] | @csv

The bit that says .[1][]? // .[1] selects the elements of the sub-array if it exists, otherwise it selects the second element of the array (and assumes that it's a scalar instead).

On the command line:

jq -r '.[].results[] | .[0] as $name | .[1][]? // .[1] | [ $name, . ] | @csv' file

Result, given your example document:

"sm-clust001","163slesm02"
"sm-clust001","163slesm01"
"sm-cssl112","ucsbnchac240"
"sm-cssl112","ucsbnchac209"
"sm-cssl112","ucsbnchac241"
"sm-cssl112","ucsbnchac242"
"ASite","unixhost1123"

.[].results[] is a set of arrays. In each array, the first element is what you want to have in the first column, and the second element is another array that we want to loop over.

So let's keep track of the first element in $name (assuming this is a cluster name of some sort), and then output this together with each element of the sub-array:

.[].results[] | .[0] as $name | .[1][]? // .[1] | [ $name, . ] | @csv

The bit that says .[1][]? // .[1] selects the elements of the sub-array if it exists, otherwise it selects the second element of the array (and assumes that it's a scalar instead).

On the command line:

jq -r '.[].results[] | .[0] as $name | .[1][]? // .[1] | [ $name, . ] | @csv' file

Result, given your example document:

"sm-clust001","163slesm02"
"sm-clust001","163slesm01"
"sm-cssl112","ucsbnchac240"
"sm-cssl112","ucsbnchac209"
"sm-cssl112","ucsbnchac241"
"sm-cssl112","ucsbnchac242"
"ASite","unixhost1123"

This solution is generalized for any number of columns in my answer to the user's followup question.

added 453 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

.[].results[] is a set of arrays. In each array, the first element is what you want to have in the first column, and the second element is another array that we want to loop over.

So let's keep track of the first element in $name (assuming this is a cluster name of some sort), and then output this together with each element of the sub-array:

.[].results[] | .[0] as $name | .[1][]? // .[1] | [ $name, . ] | @csv

The bit that says .[1][]? // .[1] selects the elements of the sub-array if it exists, otherwise it selects the second element of the array (and assumes that it's a scalar instead).

On the command line:

jq -r '.[].results[] | .[0] as $name | .[1][]? // .[1] | [ $name, . ] | @csv' file

Result, given your example document:

"sm-clust001","163slesm02"
"sm-clust001","163slesm01"
"sm-cssl112","ucsbnchac240"
"sm-cssl112","ucsbnchac209"
"sm-cssl112","ucsbnchac241"
"sm-cssl112","ucsbnchac242"
"ASite","unixhost1123"

.[].results[] is a set of arrays. In each array, the first element is what you want to have in the first column, and the second element is another array that we want to loop over.

So let's keep track of the first element in $name (assuming this is a cluster name of some sort), and then output this together with each element of the sub-array:

.[].results[] | .[0] as $name | .[1][] | [ $name, . ] | @csv

On the command line:

jq -r '.[].results[] | .[0] as $name | .[1][] | [ $name, . ] | @csv' file

.[].results[] is a set of arrays. In each array, the first element is what you want to have in the first column, and the second element is another array that we want to loop over.

So let's keep track of the first element in $name (assuming this is a cluster name of some sort), and then output this together with each element of the sub-array:

.[].results[] | .[0] as $name | .[1][]? // .[1] | [ $name, . ] | @csv

The bit that says .[1][]? // .[1] selects the elements of the sub-array if it exists, otherwise it selects the second element of the array (and assumes that it's a scalar instead).

On the command line:

jq -r '.[].results[] | .[0] as $name | .[1][]? // .[1] | [ $name, . ] | @csv' file

Result, given your example document:

"sm-clust001","163slesm02"
"sm-clust001","163slesm01"
"sm-cssl112","ucsbnchac240"
"sm-cssl112","ucsbnchac209"
"sm-cssl112","ucsbnchac241"
"sm-cssl112","ucsbnchac242"
"ASite","unixhost1123"
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

.[].results[] is a set of arrays. In each array, the first element is what you want to have in the first column, and the second element is another array that we want to loop over.

So let's keep track of the first element in $name (assuming this is a cluster name of some sort), and then output this together with each element of the sub-array:

.[].results[] | .[0] as $name | .[1][] | [ $name, . ] | @csv

On the command line:

jq -r '.[].results[] | .[0] as $name | .[1][] | [ $name, . ] | @csv' file