Monday 21 February 2022

Yet more fun with jq, this time it's gosh-darned hyphens

I'm working with Kubernetes atm, and was trying / failing to retrieve a specific Secret

This was what I was running: -

kubectl get secret bootstrap-token-abcdef --namespace kube-system --output JSON | jq .data.token-secret    

but jq was borking with: -

jq: error: secret/0 is not defined at <top-level>, line 1:

.data.token-secret            

jq: 1 compile error

whereas other similar keys were being retrieved just fine: -

kubectl get secret bootstrap-token-abcdef --namespace kube-system --output JSON | jq .data.description 

This occurred whether I was using Zsh or Bash as my Mac's shell

Note the difference ?

Yes, the working key does NOT have a hyphen ( - ) whereas the failing key surely does ...

This helped: -


namely: -

You can use ."" syntax, like jq '."app-code"'

Hmm, I thought, and tried that: -

kubectl get secret bootstrap-token-abcdef --namespace kube-system --output JSON | jq '.data."token-secret"'

Yay!

I also wanted the output in raw format, so added in a -r switch: -

kubectl get secret bootstrap-token-abcdef --namespace kube-system --output JSON | jq -r '.data."token-secret"'

Good to know

No comments:

Visual Studio Code - Wow 🙀

Why did I not know that I can merely hit [cmd] [p]  to bring up a search box allowing me to search my project e.g. a repo cloned from GitHub...