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.
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"
]
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).