Home > front end >  parsing kubectl json output with jq or jsonpath
parsing kubectl json output with jq or jsonpath

Time:03-09

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?

CodePudding user response:

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

CodePudding user response:

You could base a jq solution on:

.spec.versions[] | select(.name | contains("v1beta1"))

or similar, e.g.

.spec.versions[] | select(.name | startswith("v1beta1"))

  • Related