jq:json 个对象的输出数组
jq: output array of json objects
假设我有输入:
{
"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"
}
]
我在文档中也没有看到数组输出的选项,感谢任何帮助
使用 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"
}
]
假设我有输入:
{
"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"
}
]
我在文档中也没有看到数组输出的选项,感谢任何帮助
使用 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"
}
]