使用jq,如何替换特定键的值?

sou*_*ala 7 replace json jq

如何替换xxxyyy

{
    "spec": {
        "template": {
            "spec": {
                "containers": [{
                    "args": [
                        "proxy",
                        "router",
                        "--domain",
                        "$(POD_NAMESPACE).svc.cluster.local",
                        "--proxyLogLevel=warning",
                        "--proxyComponentLogLevel=misc:error",
                        "--log_output_level=default:info",
                        "--serviceCluster",
                        "istio-ingressgateway"
                    ],
                    "env": [{
                            "name": "JWT_POLICY",
                            "value": "third-party-jwt"
                        },
                        {
                            "name": "ISTIO_META_OWNER",
                            "value": "kubernetes://apis/apps/v1/namespaces/istio-system/deployments/xxx"
                        }
                    ]
                }]
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

EWJ*_*J00 5

以下是您可以使用以下方法专门更改.spec.template.spec.containers[].env[].valueISTIO_META_OWNER 的方法|= sub()

下面是替换的应用方式:

jq -r '.spec.template.spec.containers[].env[] | (select(.name=="ISTIO_META_OWNER") |.value |= sub("xxx$"; "yyy"))' kubernetes_spec_example.json

Run Code Online (Sandbox Code Playgroud)

结果如下:

{
  "name": "ISTIO_META_OWNER",
  "value": "kubernetes://apis/apps/v1/namespaces/istio-system/deployments/yyy"
}
Run Code Online (Sandbox Code Playgroud)