Home > Software engineering >  Getting just a string from a list
Getting just a string from a list

Time:12-18

How do I just get 1 output from "labels"?

tried doing -o=jsonpath='{.metadata.labels[0]}' in hopes of getting the first string but that threw an error.

 "metadata": {
        "labels": {
            "beta.kubernetes.io/arch": "amd64",
            "beta.kubernetes.io/os": "linux",
            "kubernetes.io/arch": "amd64",
            "kubernetes.io/hostname": "143.110.156.190",
            "kubernetes.io/os": "linux",
            "node-role.kubernetes.io/controlplane": "true",
            "node-role.kubernetes.io/etcd": "true",
            "node-role.kubernetes.io/worker": "true"
        },

CodePudding user response:

metadata.labels is not an array, so don't think '{.metadata.labels[0]}' will work.

One of the option is perhaps you can try is to use shell to fetch the first value as following:

kubectl get ingress -o json | jq '.items[0].metadata.labels' | head -2 |tr -d , |cat - <(echo "}") | jq

CodePudding user response:

Kubectl uses JSONPath expressions to filter on specific fields in the JSON object and format the output:

kubectl get ingress -o=jsonpath='{.items[0].metadata.labels}'

For reference use the following link: https://kubernetes.io/docs/reference/kubectl/jsonpath/

  • Related