Use JSONPATH to get configmap value Use JSONPATH to get configmap value kubernetes kubernetes

Use JSONPATH to get configmap value


Escape the . inside single quotes

kubectl get cm -l app=haproxy -o jsonpath="{.items[0].data['haproxy\.cfg']}"

* This didn't work a long time ago, pre 1.5. Then you needed to use go-template formatting.


I have created simple configmap.

kubectl get cm game-config-example -o json

returns:

{    "apiVersion": "v1",    "data": {        "game.properties": "enemies=aliens\nlives=3\nenemies.cheat=true\nenemies.cheat.level=noGoodRotten\nsecret.code.passphrase=UUDDLRLRBABAS\nsecret.code.allowed=true\nsecret.code.lives=30",        "ui.properties": "color.good=purple\ncolor.bad=yellow\nallow.textmode=true\nhow.nice.to.look=fairlyNice\n"    },    "kind": "ConfigMap",    "metadata": {        "creationTimestamp": "2021-06-16T10:08:28Z",        "name": "game-config-example",        "namespace": "default",        "resourceVersion": "24666141",        "selfLink": "/api/v1/namespaces/default/configmaps/game-config-example",        "uid": "3d6d2ba0-8f5a-43a7-953b-91a62dbcd248"    }}

I have tested solution with escaping . characters on versions 1.19 and 1.21. Both works fine.

kubectl get cm game-config-example -o jsonpath="{['data']['ui\.properties']}"

gives right output:

color.good=purplecolor.bad=yellowallow.textmode=truehow.nice.to.look=fairlyNice

Same result I can achive if I use:

  • go-template:
kubectl get cm game-config-example -o 'go-template={{index .data "ui.properties" }}'
  • jq command:
kubectl get cm game-config-example -o json | jq -r '.data."ui.properties"'

You can see also this issue - JSONpath fails to return keys containing dots in a map.