Answers: <p>Since the result is JSON, pipe the result to <a href="https://stedolan.github.io/jq/" rel="nofollow noreferrer"><strong>jq</strong></a> and go from there.</p>
  <pre><code>kubectl get | jq '.items[0].metadata.name'
  </code></pre>
  <p>yields</p>
  <pre><code>➜ cat abc.json | jq '.items[0].metadata.name'
  &quot;kubernetes-bootcamp-5c69669756-cxhwf&quot;
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2018-11-29T10:01:39.700Z
Answers: 2021-05-11T23:22:44.343Z
Answers: 2021-05-11T23:22:44.343Z
Answers: Kyslik
Answers: <p>The syntax of the command is as follows;</p>
  <pre><code>$ kubectl get pods &lt;optional_pod_name&gt; --output custom-columns=&quot;NAME:.metadata.name&quot;
  NAME
  kubernetes-bootcamp-5c69669756-cxhwf
  </code></pre>
  <p>You can add more columns by separating them with commas. For example;</p>
  <pre><code>$ kubectl get pods &lt;optional_pod_name&gt; -o custom-columns=&quot;NAME:.metadata.name,STATUS:.status.phase&quot;
  
  NAME                                   STATUS
  kubernetes-bootcamp-5c69669756-cxhwf   Running
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2023-10-10T10:53:47.693Z
Answers: 2023-10-10T10:53:47.693Z
Answers: yildirim
Answers: <p>You can also use <a href="https://kubernetes.io/docs/reference/kubectl/jsonpath/" rel="nofollow noreferrer">JSONPath</a>:</p>
  <pre class="lang-sh prettyprint-override"><code>kubectl get nodes --output=custom-columns='$COLUMN_NAME:$JSON_PATH'
  kubectl get nodes --output=custom-columns='NAME:{.metadata.name}' # optional curly bracket
  kubectl get nodes --output=custom-columns='NAME:.metadata.name,POD_CIDR:{.spec.podCIDR}'
  kubectl get nodes --output=jsonpath='{range .items[*]}{.metadata.name}{&quot; &quot;}{.spec.podCIDR}{&quot;\n&quot;}{end}'
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-03-07T16:46:29.533Z
Answers: 2024-03-07T16:52:29.300Z
Answers: 2024-03-07T16:52:29.300Z
Answers: piouson
Body: <p>I want to use <code>kubectl get -o=custom-columns</code> to extract information with specific columns. The cropped yaml response is like this</p>
  
  <pre><code>{
      "apiVersion": "v1",
      "items": [
          {
              "apiVersion": "v1",
              "kind": "Pod",
              "metadata": {
                  "creationTimestamp": "2018-11-27T14:37:46Z",
                  "generateName": "kubernetes-bootcamp-5c69669756-",
                  "labels": {
                      "pod-template-hash": "1725225312",
                      "run": "kubernetes-bootcamp"
                  },
                  "name": "kubernetes-bootcamp-5c69669756-cxhwf",
                  "namespace": "default",
                  "ownerReferences": [
                      {
                          "apiVersion": "extensions/v1beta1",
                          "blockOwnerDeletion": true,
                          "controller": true,
                          "kind": "ReplicaSet",
                          "name": "kubernetes-bootcamp-5c69669756",
                          "uid": "950aac8e-f24f-11e8-bf5d-065b6ba59aa8"
                      }
                  ],
                  "resourceVersion": "118035",
                  "selfLink": "/api/v1/namespaces/default/pods/kubernetes-bootcamp-5c69669756-cxhwf",
                  "uid": "0190c234-f252-11e8-bf5d-065b6ba59aa8"
              },
  </code></pre>
  
  <p>I want to extract the <strong>name</strong> from items[0].metadata.name, but I don't know the syntax.</p>
  
  <p>My questions:</p>
  
  <ol>
  <li>What is the query syntax used in this situation. Is it JSON path? I don't see it documented.</li>
  <li>What is the specific query in my case?</li>
  </ol>
  
Tags: <kubernetes>
Title: How to specify a filter for Kubectl -o flag=custom-column
UserDisplayName: Tran Triet
ThreadText: # How to specify a filter for Kubectl -o flag=custom-column
  
  (Asked by: username_0 on 2018-11-28)
  
  I want to use `kubectl get -o=custom-columns` to extract information with specific columns. The cropped yaml response is like this
  
  ```
  {
      "apiVersion": "v1",
      "items": [
          {
              "apiVersion": "v1",
              "kind": "Pod",
              "metadata": {
                  "creationTimestamp": "2018-11-27T14:37:46Z",
                  "generateName": "kubernetes-bootcamp-5c69669756-",
                  "labels": {
                      "pod-template-hash": "1725225312",
                      "run": "kubernetes-bootcamp"
                  },
                  "name": "kubernetes-bootcamp-5c69669756-cxhwf",
                  "namespace": "default",
                  "ownerReferences": [
                      {
                          "apiVersion": "extensions/v1beta1",
                          "blockOwnerDeletion": true,
                          "controller": true,
                          "kind": "ReplicaSet",
                          "name": "kubernetes-bootcamp-5c69669756",
                          "uid": "950aac8e-f24f-11e8-bf5d-065b6ba59aa8"
                      }
                  ],
                  "resourceVersion": "118035",
                  "selfLink": "/api/v1/namespaces/default/pods/kubernetes-bootcamp-5c69669756-cxhwf",
                  "uid": "0190c234-f252-11e8-bf5d-065b6ba59aa8"
              },
  
  ```
  
  I want to extract the **name** from items[0].metadata.name, but I don't know the syntax.
  
  My questions:
  
  1. What is the query syntax used in this situation. Is it JSON path? I don't see it documented.
  2. What is the specific query in my case?
  
  **Tags:** kubernetes
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2018-11-29. Score: 2
  
  Since the result is JSON, pipe the result to [**jq**](https://stedolan.github.io/jq/) and go from there.
  
  ```
  kubectl get | jq '.items[0].metadata.name'
  
  ```
  
  yields
  
  ```
  ➜ cat abc.json | jq '.items[0].metadata.name'
  "kubernetes-bootcamp-5c69669756-cxhwf"
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2023-10-10. Score: 0
  
  The syntax of the command is as follows;
  
  ```
  $ kubectl get pods <optional_pod_name> --output custom-columns="NAME:.metadata.name"
  NAME
  kubernetes-bootcamp-5c69669756-cxhwf
  
  ```
  
  You can add more columns by separating them with commas. For example;
  
  ```
  $ kubectl get pods <optional_pod_name> -o custom-columns="NAME:.metadata.name,STATUS:.status.phase"
  
  NAME                                   STATUS
  kubernetes-bootcamp-5c69669756-cxhwf   Running
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2024-03-07. Score: 0
  
  You can also use [JSONPath](https://kubernetes.io/docs/reference/kubectl/jsonpath/):
  
  ```sh
  kubectl get nodes --output=custom-columns='$COLUMN_NAME:$JSON_PATH'
  kubectl get nodes --output=custom-columns='NAME:{.metadata.name}' # optional curly bracket
  kubectl get nodes --output=custom-columns='NAME:.metadata.name,POD_CIDR:{.spec.podCIDR}'
  kubectl get nodes --output=jsonpath='{range .items[*]}{.metadata.name}{" "}{.spec.podCIDR}{"\n"}{end}'
  
  ```
  
  
  ---
  
  