Search code examples
kubernetesjqjsonpath

parsing kubectl json output with jq or jsonpath


I would like to select, and list the crds which are containing the "v1beta1" in the

.spec.versions.*.name

The versions part of the crd object looks similar like this

    "versions": [
      {
        "name": "v1alpha2",
        "served": true,
        "storage": true,
        "subresources": {
          "status": {}
        },
        "name": "v1beta1"
        "served": true,
        "storage": true,
        "subresources": {
          "status": {}
        }
      }
    ]

I tried some different queries like the following, but no success.

$ kubectl get crd -ojson | jq -r '.items[] | map(select(.spec.versions[] | contains("v1beta1"))).metadata.name'
jq: error (at <stdin>:250345): Cannot index string with string "spec"

Jsonpath solution would be also great. I tried something like this without success.

$ kubectl get crd -ojsonpath="{range .items.*.spec.versions.*}{.name[?(@=='v1beta1')].metadata.name}{'\n'}{end}"

Could someone help me please?


Solution

  • This will show the name using jsonpath: kubectl get crd -o jsonpath='{range .items[?(@.spec.versions[].name=="v1beta1")].metadata}{.name}{"\n"}'