JQ |更新由select选择的数组元素 [英] JQ | Updating array element selected by `select`

查看:43
本文介绍了JQ |更新由select选择的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JSON数组中,我想根据节点的值选择一个数组元素,然后更新同一数组元素中的另一个节点.例如.在下面的JSON中:

In a JSON array, I want to select an array element on basis of a node's value, then update a different node in the same array element. E.g. in the JSON below:

{
"apiVersion": "vlabs",
"properties": {
    "orchestratorProfile": {
    "orchestratorType": "Kubernetes",
    "orchestratorRelease": "1.7",
    "orchestratorVersion": "1.7.10",
    "kubernetesConfig": {
        "kubernetesImageBase": "gcrio.azureedge.net/google_containers/",
        "clusterSubnet": "10.105.208.0/20",
        "networkPolicy": "calico",
        "nonMasqueradeCidr": "10.0.0.0/8",
        "maxPods": 110,
        "dockerBridgeSubnet": "172.17.0.1/16"
        "addons": [
        {
            "name": "tiller",
            "enabled": true
        },
        {
            "name": "aci-connector",
            "enabled": true
        },
        {
            "name": "kubernetes-dashboard",
            "enabled": true
        },
        {
            "name": "rescheduler",
            "enabled": true
        }
        ]
    }
    }
}
}

我想禁用所有不是重新安排程序"的插件,即为数组.properties.orchestratorProfile.kubernetesConfig.addons[]的元素设置.enabled = false,其中.name != "rescheduler".我能解决的最接近的是

I want to disable all addons which are not "rescheduler", i.e. set .enabled = false for elements of array .properties.orchestratorProfile.kubernetesConfig.addons[] where .name != "rescheduler". Closest I could work out was

jq -r '.properties.orchestratorProfile.kubernetesConfig.addons[] |
  select (.name != "rescheduler" ) | .enabled =  false'

但是这种方式,或者我尝试过的其他方式,总是会丢失数组之外的数据.

but this, or any other ways I tried, I always lose the data outside of the array.

预期结果是:

{
"apiVersion": "vlabs",
"properties": {
    "orchestratorProfile": {
    "orchestratorType": "Kubernetes",
    "orchestratorRelease": "1.7",
    "orchestratorVersion": "1.7.10",
    "kubernetesConfig": {
        "kubernetesImageBase": "gcrio.azureedge.net/google_containers/",
        "clusterSubnet": "10.105.208.0/20",
        "networkPolicy": "calico",
        "nonMasqueradeCidr": "10.0.0.0/8",
        "maxPods": 110,
        "dockerBridgeSubnet": "172.17.0.1/16"
        "addons": [
        {
            "name": "tiller",
            "enabled": false
        },
        {
            "name": "aci-connector",
            "enabled": false
        },
        {
            "name": "kubernetes-dashboard",
            "enabled": false
        },
        {
            "name": "rescheduler",
            "enabled": true
        }
        ]
    }
    }
}
}

我该怎么做?任何想法,帮助或指导均应事先获得感谢.

How do I go about doing this? Any idea or help or guidance is appreciated in advance.

推荐答案

您的jq查询很方便,除了缺少一对括号外:

Your jq query is spot-on except essentially for a missing pair of parentheses:

(.properties.orchestratorProfile.kubernetesConfig.addons[]
 | select (.name != "rescheduler" ).enabled) = false

也就是说,在分配的LHS上,您需要指定需要更新的值的路径.

That is, on the LHS of the assignment, you need to specify the paths of the values that need to be updated.

这篇关于JQ |更新由select选择的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆