【问题标题】:Use jq to select objects not containing nested objects使用 jq 选择不包含嵌套对象的对象
【发布时间】:2017-09-18 13:52:16
【问题描述】:

我有以下 JSON:

{
"apiVersion": "v1",
"items": [
    {
        "apiVersion": "v1",
        "kind": "Node",
        "metadata": {
            "annotations": {
                "node.alpha.kubernetes.io/ttl": "0",
                "volumes.kubernetes.io/controller-managed-attach-detach": "true"
            },
            "creationTimestamp": "2017-09-14T11:53:07Z",
            "labels": {
                "beta.kubernetes.io/arch": "amd64",
                "beta.kubernetes.io/os": "linux",
                "kubernetes.io/hostname": "msl-kub01.int.na.myapp.com",
                "node-role.kubernetes.io/master": ""
            },
            "name": "msl-kub01.int.na.myapp.com",
            "namespace": "",
            "resourceVersion": "123154",
            "selfLink": "/api/v1/nodes/msl-kub01.int.na.myapp.com",
            "uid": "45e3b430-9943-11e7-bf0b-fa163e6604fc"
        },
        "spec": {
            "externalID": "msl-kub01.int.na.myapp.com",
            "taints": [
                {
                    "effect": "NoSchedule",
                    "key": "node-role.kubernetes.io/master",
                    "timeAdded": null
                }
            ]
        }
    },    
    {
        "apiVersion": "v1",
        "kind": "Node",
        "metadata": {
            "annotations": {
                "node.alpha.kubernetes.io/ttl": "0",
                "volumes.kubernetes.io/controller-managed-attach-detach": "true"
            },
            "creationTimestamp": "2017-09-14T12:05:42Z",
            "labels": {
                "beta.kubernetes.io/arch": "amd64",
                "beta.kubernetes.io/os": "linux",
                "kubernetes.io/hostname": "msl-kub02.int.na.myapp.com"
            },
            "name": "msl-kub02.int.na.myapp.com",
            "namespace": "",
            "resourceVersion": "123156",
            "selfLink": "/api/v1/nodes/msl-kub02.int.na.myapp.com",
            "uid": "084f439e-9945-11e7-bf0b-fa163e6604fc"
        },
        "spec": {
            "externalID": "msl-kub02.int.na.myapp.com"
        }
    }
],
"kind": "List",
"metadata": {
    "resourceVersion": "",
    "selfLink": ""
}}

我需要选择“items”列表中没有“spec.taints[].effect == “NoSchedule”的所有条目。

问题是在源 JSON 中存在污点列表和效果键不是强制性的,所以我不能这样做:

select (.spec.taints[].effect != "NoSchedule")

我希望使用类似的东西:

select (has(".spec.taints[].effect") | not)

但这是不允许的。

谢谢提示。

【问题讨论】:

  • 发布最终预期结果

标签: json select jq


【解决方案1】:

jq解决方案:

jq '[.items[] | if (.spec | has("taints") | not) 
    or (.spec.taints[] | select(.effect!="NoSchedule")) then . else empty end]' your.json

输出:

[
  {
    "apiVersion": "v1",
    "kind": "Node",
    "metadata": {
      "annotations": {
        "node.alpha.kubernetes.io/ttl": "0",
        "volumes.kubernetes.io/controller-managed-attach-detach": "true"
      },
      "creationTimestamp": "2017-09-14T12:05:42Z",
      "labels": {
        "beta.kubernetes.io/arch": "amd64",
        "beta.kubernetes.io/os": "linux",
        "kubernetes.io/hostname": "msl-kub02.int.na.myapp.com"
      },
      "name": "msl-kub02.int.na.myapp.com",
      "namespace": "",
      "resourceVersion": "123156",
      "selfLink": "/api/v1/nodes/msl-kub02.int.na.myapp.com",
      "uid": "084f439e-9945-11e7-bf0b-fa163e6604fc"
    },
    "spec": {
      "externalID": "msl-kub02.int.na.myapp.com"
    }
  }
]

【讨论】:

    【解决方案2】:

    如果给定的属性或数组不存在,您可以使用? 忽略任何错误。因此,考虑到这一点,检查其中没有一个包含"NoSchedule" 污染效果的项目。

    .items[] | select(all(.spec.taints[]?; .effect != "NoSchedule"))
    

    【讨论】:

      【解决方案3】:

      这是一个应该可以工作的过滤器。

         .items[]
       | select(.spec.taints | (.==null) or (any(.effect=="NoSchedule")|not))
      

      【讨论】:

        猜你喜欢
        • 2023-01-09
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-13
        • 1970-01-01
        • 2018-06-02
        • 2018-01-21
        相关资源
        最近更新 更多