小编dba*_*ett的帖子

使用 jq 有条件地修改嵌套 json

我想修改如下 JSON:

{
  "foobar": {
    "a": {
      "adkjfe": {
        "A": 1,
        "foo": "bar"
      }
    },
    "b": {
      "ekjaei": {
        "A": 2,
        "bar": "foo"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如果 A=1,则添加更多数据{"baz": ["bing", "bop"]}到 A 的父级。假设我不知道父键,同时保持 json 的其余部分不变。我尝试过很多不同的事情,包括:

.foobar | .. | .. | .[] | if select(.A==1) then . += {"baz": "bing"} else . end 
Run Code Online (Sandbox Code Playgroud)

这给了我一个错误,并且只有我修改过的部分。

在这种情况下,我希望看到的结果是:

{
  "foobar": {
    "a": {
      "adkjfe": {
        "A": 1,
        "foo": "bar",
        "baz": ["bing", "bop"]
      }
    },
    "b": {
      "ekjaei": {
        "A": 2,
        "bar": …
Run Code Online (Sandbox Code Playgroud)

json nested updates jq

5
推荐指数
1
解决办法
318
查看次数

标签 统计

jq ×1

json ×1

nested ×1

updates ×1