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

How to put jq output multi arrays in single array

While reading data from a json file using jq.
This command jq ".[]|keys" config.json gives following output:

[
  "N1",
  "N2"
]
[
  "N3",
  "N4"
]

What I ideally want is very simple:

["N1", "N2", "N3", "N4"]

However,
How I cannot find a way to accomplish this.

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

Config file:

{
    "Test1":{
        "N1":{
            "crn":"1",
            "con":"2"
        },
        "N2":{
            "crn":"100",
            "con":"200"
        }
        
    },
    "test2":{
        "N3":{
            "crn":"xx",
            "con":"2x"
        },
        "N4":{
            "crn":"xxxx",
            "con":"3xx"
        }
    }
}

>Solution :

Use map instead:

jq 'map(keys[])' config.json
[
  "N1",
  "N2",
  "N3",
  "N4"
]

Demo

Note that keys sorts the keys. If you want them unsorted, use keys_unsorted instead.

Also, if you want to have the output in one line, add the --compact-output (or -c) option (thx @Cyrus).

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