说我有输入:
{
"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"
}
]
我也没有看到文档中数组输出的选项,任何帮助赞赏
答案 0 :(得分:28)
使用slurp模式:
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"
}
]