Jq: json 对象的输出数组

假设我有输入:

{
"name": "John",
"email": "john@company.com"
}
{
"name": "Brad",
"email": "brad@company.com"
}

如何得到输出:

[
{
"name": "John",
"email": "john@company.com"
},
{
"name": "Brad",
"email": "brad@company.com"
}
]

我两种方法都试过了:

jq '[. | {name, email}]'

还有

jq '. | [{name, email}]'

它们都给了我输出

[
{
"name": "John",
"email": "john@company.com"
}
]
[
{
"name": "Brad",
"email": "brad@company.com"
}
]

我在文档中也没有看到数组输出的选项,感谢您的帮助

75434 次浏览

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": "john@company.com"
},
{
"name": "Brad",
"email": "brad@company.com"
}
]