How to extract json's values as JSON array with jq?
For example for the below json:
{
"a" : 1,
"b" : 2
}
I would like to extract values as JSON array:
[
1,
2
]
How to do it with jq?
I found a way to extract keys only in the docs:
echo $json | jq keys returns:
[
"a",
"b"
]
echo $json | jq '[.[]]'
output:
[
1,
2
]
One solution is to use to_entries then map to extract only the values:
$ echo '{
"a" : 1,
"b" : 2
}' | jq 'to_entries|map(.value)'
[
1,
2
]
Another solution, simpler and faster, is to use just map(.):
$ echo '{
"a" : 1,
"b" : 2
}' | jq 'map(.)'
[
1,
2
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With