首页 > 解决方案 > 使用 jq 替换 json 中的争论

问题描述

{
"containers": [
          {
            "args": [
              "water",
              "fight",
              "--homein",
              "$(POD_NAMESPACE).svc.cluster.local",
              "--proxyLogLevel=warning",
              "--proxyComponentLogLevel=misc:error",
              "--log_output_level=default:info",
              "ms-madison",
              "--trust-domain=cluster.local"
            ]}]
}

我可以用一个丑陋的解决方案 .containers[0].args[8] |= "mr-harmison"' 用 mr-harmison 替换 ms-madison

ms-madison 可以出现在任何位置。你能建议我一个更好的方法来处理这个问题吗?

标签: jsonjqedit

解决方案


一个平淡但强大的解决方案:

(.containers[0].args | index("ms-madison")) as $ix
| if $ix then .containers[0].args[$ix] = "mr-harmison" else . end

推荐阅读