如何使用jq将JSON对象流转换为数组 [英] How to convert a JSON object stream into an array with jq
本文介绍了如何使用jq将JSON对象流转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用jq
将json对象流放入json数组中,例如从
I want to use jq
to put a stream of json objects into a json array, for example, from
{"a":1}
{"b":2}
到
[{"a":1},
{"b":2}]
但这不起作用
echo '
{"a":1}
{"b":2}
'|jq '[.]'
自从我得到
[
{
"a": 1
}
]
[
{
"b": 2
}
]
推荐答案
使用-s
选项将其吸收.
$ jq -s '.' <<< '{ "a": 1 } { "b": 2 }'
[
{
"a": 1
},
{
"b": 2
}
]
作为另一种选择,使用inputs
读取值是更灵活的选择.通常,您通常希望将此选项与-n
选项结合使用,以防止第一个值过早地被消耗.
As another option, reading the values using inputs
is a much more flexible alternative. You'll usually want to use this in conjunction with the -n
option to prevent the first value from being consumed prematurely.
$ jq -n '[inputs]' <<< '{ "a": 1 } { "b": 2 }'
这篇关于如何使用jq将JSON对象流转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文