Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use jq command to display a two dimensional JSON array

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1 2 3
4 5
6 7 8 9

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading