jq:更改多个值

时间:2017-11-17 17:09:32

标签: json edit jq

我正在尝试使用此行更改多个json值

 jq '.two="newval", .three="newval"' my.json 

这是输入

{
  "one": {
    "val": 1
  },
  "two": "val",
  "three": "val",
  "four": "val"
}

但输出为2 jsons:

{
  "one": {
    "val": 1
  },
  "two": "newval",
  "three": "val",
  "four": "val"
}
{
  "one": {
    "val": 1
  },
  "two": "val",
  "three": "newval",
  "four": "val"
}

如何在一个项目中更改多个值和输出?

2 个答案:

答案 0 :(得分:13)

只需将逗号更改为竖线字符就可以了:

untap

“,”用于连接流:.two="newval" | .three="newval" 将发出A,然后是B.

答案 1 :(得分:4)

以下是使用+ object addition更新多个成员的方法。

. + {two:"newtwo", three:"newthree"}

示例运行(假设data.json中的数据)

$ jq -M '. + {two:"newtwo", three:"newthree"}' data.json
{
  "one": {
    "val": 1
  },
  "two": "newtwo",
  "three": "newthree",
  "four": "val"
}

Try it online at jqplay.org