使用jq选择不包含嵌套对象的对象

时间:2017-09-18 13:52:16

标签: json select jq

我有以下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中不存在taints列表和效果键,因此我不能这样做:

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

我希望使用类似的东西:

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

但是不允许这样做。

提示提示。

3 个答案:

答案 0 :(得分: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"
    }
  }
]

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

这是一个应该有效的过滤器。

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