[英]Append JSON Objects using jq
I've below JSON
structure 我在
JSON
结构下面
{
"a": "aVal",
"x": {
"x1": "x1Val",
"x2": "x2Val"
}
"y": {
"y1": "y1Val"
}
}
I want to add "x3": "x3Val","x4": "x4Val"
to x
. 我想将
"x3": "x3Val","x4": "x4Val"
到x
。 So the output should be 所以输出应该是
{
...
"x": {
....
"x3": "x3Val",
"x4": "x4Val",
}
...
}
Yes it is, provided you add a comma on line 8 after the closing bracket }
(otherwise jq
won't parse your input JSON data): 是的,只要您在
jq
括号}
之后的第8行添加一个逗号(否则jq
不会解析您输入的JSON数据):
$ jq '.x.x3="x3val"|.x.x4="x4val"' file
{
"a": "aVal",
"x": {
"x1": "x1Val",
"x2": "x2Val",
"x3": "x3val",
"x4": "x4val"
},
"y": {
"y1": "y1Val"
}
}
Alternatively if you need to pass values as argument, use the option --arg
: 或者,如果您需要将值作为参数传递,请使用
--arg
选项:
jq --arg v3 "x3val" --arg v4 "x4val" '.x.x3=$v3|.x.x4=$v4' file
Of course, it's pretty simple for jq
: 当然,对于
jq
非常简单:
jq '.x += {"x3": "x3Val","x4": "x4Val"}' file.json
The output: 输出:
{
"a": "aVal",
"x": {
"x1": "x1Val",
"x2": "x2Val",
"x3": "x3Val",
"x4": "x4Val"
},
"y": {
"y1": "y1Val"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.