Home > Net >  Use jq command to display a two dimensional JSON array
Use jq command to display a two dimensional JSON array

Time:02-04

Is there a simple way to display a 2D JSON array using jq, so that each subarray is on its own line?

E.g. If I have this JSON:

{
  "array": [
    [1, 2, 3],
    [4, 5],
    [6, 7, 8, 9]
  ]
}

I'm trying to use jq to print out:

1 2 3
4 5
6 7 8 9

CodePudding user response:

Apply compact -c output:

$ jq -c '.array[]' test.json 
[1,2,3]
[4,5]
[6,7,8,9]

Or if you also need to join array values by space char:

$ jq -rc '.array[] | join(" ")' test.json 
1 2 3
4 5
6 7 8 9
  • Related