jq: output array of json objects [duplicate]
Say I have the input:
{
"name": "John",
"email": "[email protected]"
}
{
"name": "Brad",
"email": "[email protected]"
}
How do I get the output:
[
{
"name": "John",
"email": "[email protected]"
},
{
"name": "Brad",
"email": "[email protected]"
}
]
I tried both:
jq '[. | {name, email}]'
and
jq '. | [{name, email}]'
which both gave me the output
[
{
"name": "John",
"email": "[email protected]"
}
]
[
{
"name": "Brad",
"email": "[email protected]"
}
]
I also saw no options for an array output in the documentations, any help appreciated
Solution 1:
Use slurp mode:
o --slurp/-s: Instead of running the filter for each JSON object in the input, read the entire input stream into a large array and run the filter just once.
$ jq -s '.' < tmp.json
[
{
"name": "John",
"email": "[email protected]"
},
{
"name": "Brad",
"email": "[email protected]"
}
]