Search code examples
kuberneteskubectljsonpath

Getting just a string from a list


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"
        },

Solution

  • 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