<p>I need cluster-id using kubectl command.</p>

<pre><code>root@vagrant-xenial64:~# kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json}'
{
  "cluster_id": "xxx",
  "cluster_name": "prod-yyy-mmm",
  "cluster_type": "rrr",
  "cluster_pay_tier": "vvv",
  "datacenter": "cse",
  "account_id": "456777",
  "created": "2018-06-32323dffdf:35:48+0000"
}
</code></pre>

<p>I need <code>cluster-id</code> of this particular json</p>

<pre><code>root@vagrant-xenial64:~# kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json.cluster_id}'
root@vagrant-xenial64:~# 
</code></pre>

<p>Above command returns empty string.
I tried many other combinations too</p>

ambikanair
<p>Your <code>ConfigMap</code> resource <code>data</code>-field contains a string which are interpreted as-is when you run <code>jsonpath</code> to select it via <code>'{.data.cluster-config\.json}'</code>. What i mean is that the shell you use will print it as JSON at <code>stdout</code> although it's stored differently in Kubernetes. If you run <code>kubectl get cm cluster-info -n kube-system -o json</code> and look at the <code>data</code>-field it might look something like this:</p>

<pre><code>"data": {
    "cluster-config.json": "{\n  \"cluster_id\": \"xxx\",\n  \"cluster_name\": \"prod-yyy-mmm\",\n  \"cluster_type\": \"rrr\",\n  \"cluster_pay_tier\": \"vvv\",\n  \"datacenter\": \"cse\",\n  \"account_id\": \"456777\",\n  \"created\": \"2018-06-32323dffdf:35:48+0000\"\n}\n"
}
</code></pre>

<p>You won't be able to access the "fields" within that string with <code>jsonpath</code> since it's not actually part of the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#configmap-v1-core" rel="noreferrer"><code>ConfigMap</code> API resource</a> fields.</p>

<p>You could try to use a second tool to fetch it though, using <a href="https://stedolan.github.io/jq/" rel="noreferrer"><code>jq</code></a>, a command-line JSON processor. This tool would interpret the output of <code>jsonpath</code> as JSON on the fly and parse it accordingly. </p>

<p>Example:</p>

<pre><code>kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json}' | jq '.cluster_id'
"xxx"
</code></pre>

<p>If installing e.g. <code>jq</code> defeat any purposes i would recommend to use a combination of already available tools (assuming you're on Linux) like <code>grep</code>, <code>awk</code> and <code>sed</code>:</p>

<pre><code>kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json}' | grep cluster_id | awk '{ print $2 }' | sed -e 's/"//' -e 's/",//'
xxx
</code></pre>
