提取嵌套数组中的相关值

时间:2016-07-30 12:45:40

标签: jq

我想从这个json中获取一个值

{
  "kind": "List",
  "apiVersion": "v1",
  "metadata": {},
  "items": [
    {
      "kind": "Pod",
      "apiVersion": "v1",
      "metadata": {
        "name": "provisioner-secrets-1750468340-ei8sl",
        "generateName": "provisioner-secrets-1750468340-",
        "namespace": "default",
        "selfLink": "/api/v1/namespaces/default/pods/provisioner-secrets-1750468340-ei8sl",
        "uid": "8d2ac8e4-5642-11e6-b139-72f9d49a4327",
        "resourceVersion": "1473",
        "creationTimestamp": "2016-07-30T10:44:11Z",
        "labels": {
          "pod-template-hash": "1750468340",
          "run": "git-sync"
        },
        "annotations": {
          "kubernetes.io/created-by": "{\"kind\":\"SerializedReference\",\"apiVersion\":\"v1\",\"reference\":{\"kind\":\"ReplicaSet\",\"namespace\":\"default\",\"name\":\"provisioner-secrets-1750468340\",\"uid\":\"8d2904ba-5642-11e6-b139-72f9d49a4327\",\"apiVersion\":\"extensions\",\"resourceVersion\":\"1438\"}}\n"
        }
      },
      "spec": {
        "volumes": [
          {
            "name": "markdown",
            "emptyDir": {}
          },
          {
            "name": "klucze",
            "secret": {
              "secretName": "klucze"
            }
          },
          {
            "name": "default-token-xjc9q",
            "secret": {
              "secretName": "default-token-xjc9q"
            }
          }
        ],
        "containers": [
          {
            "name": "git-sync",
            "image": "lukaszbielinski/git-sync:0.5",
            "env": [
              {
                "name": "GIT_SYNC_REPO",
                "value": "https://github.com/lukasz-bielinski/test-secrets"
              },
              {
                "name": "GIT_SYNC_WAIT",
                "value": "10"
              },
              {
                "name": "GIT_SYNC_DEST",
                "value": "/git"
              }
            ],
            "resources": {},
            "volumeMounts": [
              {
                "name": "markdown",
                "mountPath": "/git"
              },
              {
                "name": "klucze",
                "mountPath": "/root/.ssh/klucze"
              },
              {
                "name": "default-token-xjc9q",
                "readOnly": true,
                "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount"
              }
            ],
            "terminationMessagePath": "/dev/termination-log",
            "imagePullPolicy": "IfNotPresent"
          },
          {
            "name": "secprov",
            "image": "lukaszbielinski/kubectl-image:0.19-secrets",
            "resources": {},
            "volumeMounts": [
              {
                "name": "markdown",
                "mountPath": "/srcsecrets"
              },
              {
                "name": "default-token-xjc9q",
                "readOnly": true,
                "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount"
              }
            ],
            "terminationMessagePath": "/dev/termination-log",
            "imagePullPolicy": "Always"
          }
        ],
        "restartPolicy": "Always",
        "terminationGracePeriodSeconds": 30,
        "dnsPolicy": "ClusterFirst",
        "serviceAccountName": "default",
        "serviceAccount": "default",
        "nodeName": "minikubevm",
        "securityContext": {}
      },
      "status": {
        "phase": "Running",
        "conditions": [
          {
            "type": "Initialized",
            "status": "True",
            "lastProbeTime": null,
            "lastTransitionTime": "2016-07-30T10:44:11Z"
          },
          {
            "type": "Ready",
            "status": "True",
            "lastProbeTime": null,
            "lastTransitionTime": "2016-07-30T10:44:31Z"
          },
          {
            "type": "PodScheduled",
            "status": "True",
            "lastProbeTime": null,
            "lastTransitionTime": "2016-07-30T10:44:11Z"
          }
        ],
        "hostIP": "192.168.122.230",
        "podIP": "172.17.0.10",
        "startTime": "2016-07-30T10:44:11Z",
        "containerStatuses": [
          {
            "name": "git-sync",
            "state": {
              "running": {
                "startedAt": "2016-07-30T10:44:12Z"
              }
            },
            "lastState": {},
            "ready": true,
            "restartCount": 0,
            "image": "lukaszbielinski/git-sync:0.5",
            "imageID": "docker://sha256:50f4c3009cd7072cb52d0edc5374ad973d7619ead7063482c4102fa230d16ce6",
            "containerID": "docker://3d8f44f2c1cd8139fdf23660e5c7514d37ba5e39c739087f12defcfcd0a12f6d"
          },
          {
            "name": "secprov",
            "state": {
              "running": {
                "startedAt": "2016-07-30T10:44:31Z"
              }
            },
            "lastState": {},
            "ready": true,
            "restartCount": 0,
            "image": "lukaszbielinski/kubectl-image:0.19-secrets",
            "imageID": "docker://sha256:6ebf833942e81d24d0bbc2f3e816f7620db979182ccf81a9edaf97488b512726",
            "containerID": "docker://c1837b472c0a8123c244c7e7c5b4886a85e8607e8172271447024aa647a0a564"
          }
        ]
      }
    }
  ]
}
我知道的事情:

"secretName": "klucze"    

我想要提取的东西

"name": "provisioner-secrets-1750468340-ei8sl"    

我试过

.items[] | .spec | .volumes[] | .secret | select(.secretName=="klucze")    

但我不知道如何从

获取相关值
.items[] | .metadata | .name    

请建议我该如何解决此问题

1 个答案:

答案 0 :(得分:2)

您知道如何获取您正在寻找的相应名称,只需在其中添加select即可获得所需条件。幸运的是,您的情况涉及您正在搜索的项目,而不是另一个不相关的对象。

$ jq --arg secret 'klucze' \
'.items[] | select(.spec.volumes[].secret.secretName == $secret).metadata.name' input.json