2017-09-18 135 views
0

我有以下JSON:使用JQ選擇對象不包含嵌套對象

{ 
"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": "" 
}} 

,我需要選擇不具有「項目」列表中的所有條目「 spec.taints []效果==」 。NoSchedule」

事情是污點名單和效果的關鍵在於存在不是在源JSON強制性的,所以我不能做:

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

我希望使用這樣的:

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

但這是不允許的。

Thx提示。

+0

發佈最終預期結果 – RomanPerekhrest

回答

0

你可以使用?忽略任何錯誤,如果給定的屬性或數組不存在。因此,考慮到這一點,請仔細閱讀沒有包含"NoSchedule"的污點效應的項目。

.items[] | select(all(.spec.taints[]?; .effect != "NoSchedule")) 
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" 
    } 
    } 
] 
0

這裏是一個過濾器應該工作。

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