简体   繁体   English

在JSON jq中修改键值数组

[英]Modifying array of key value in JSON jq

In case, I have an original json look like the following: 在这种情况下,我有一个原始的json看起来如下:

{
  "taskDefinition": {
    "containerDefinitions": [
      {
        "name": "web",
        "image": "my-image",
        "environment": [
          {
            "name": "DB_HOST",
            "value": "localhost"
          },
          {
            "name": "DB_USERNAME",
            "value": "user"
          }
        ]
      }
    ]
  }
}

And I would like to inplace modify the value for the matched key like so: 我想原样修改匹配键的值,如下所示:

jq '.taskDefinition.containerDefinitions[0].environment[] | select(.name=="DB_USERNAME") | .value="new"' json

I got the output 我得到了输出

{
  "name": "DB_USERNAME",
  "value": "new"
}

But I want more like in-place modify or the whole json from the original with new value modified, like this: 但我希望更多像就地修改或原始的整个json修改新值,如下所示:

{
      "taskDefinition": {
        "containerDefinitions": [
          {
            "name": "web",
            "image": "my-image",
            "environment": [
              {
                "name": "DB_HOST",
                "value": "localhost"
              },
              {
                "name": "DB_USERNAME",
                "value": "new"
              }
            ]
          }
        ]
      }
    }

Is it possible to do with jq or any known workaround? 是否可以使用jq或任何已知的解决方法?

Thank you. 谢谢。

Updated 更新

For anyone looking for editing multi-values, here is the approach I use 对于任何寻找编辑多值的人来说,这是我使用的方法

JQ=""
for e in DB_HOST=rds DB_USERNAME=xxx; do
    k=${e%=*}
    v=${e##*=}
    JQ+="(.taskDefinition.containerDefinitions[0].environment[] | select(.name==\"$k\") | .value) |= \"$v\" | "
done

jq '${JQ%??}' json

I think there should be more concise way, but this seems working fine. 我认为应该有更简洁的方式,但这似乎工作正常。

It is enough to assign to the path, if you are using |= , eg 如果您使用|= ,则分配给路径就足够了,例如

jq '
  (.taskDefinition.containerDefinitions[0].environment[] | 
   select(.name=="DB_USERNAME") | .value) |= "new"
' infile.json

Output: 输出:

{
  "taskDefinition": {
    "containerDefinitions": [
      {
        "name": "web",
        "image": "my-image",
        "environment": [
          {
            "name": "DB_HOST",
            "value": "localhost"
          },
          {
            "name": "DB_USERNAME",
            "value": "new"
          }
        ]
      }
    ]
  }
}

You might like to consider this alternative to using |= : 您可能想要考虑使用|=替代方法:

walk( if type=="object" and .name=="DB_USERNAME" 
      then .value="new" else . end)

Here is a select-free solution using |= : 这是一个使用|=的无选择解决方案:

.taskDefinition.containerDefinitions[0].environment |=
  map(if .name=="DB_USERNAME" then .value = "new"
      else . end)

Avoiding select within the expression on the LHS of |= makes the solution more robust wrt the version of jq being used. 避免在LHS的表达式中select |=使得解决方案在使用jq的版本时更加健壮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM