I am trying to extract podname using the below jq query.
❯ kubectl get pods -l app=mssql-primary --output jsonpath='{.items[0].metadata.name}'
mssqlag-primary-deployment-77b8974bb9-dbltl%
❯ kubectl get pods -l app=mssql-primary -o json | jq -r '.items[0].metadata.name'
mssqlag-primary-deployment-77b8974bb9-dbltl
While they both provide the same out put the first one has a % character at the end of the pod name. Any reason why ? Is there something wrong with the jsonpath representation in the first command ?
CodePudding user response:
I reproduced your case with the same deployment name and label. And I get the same output from both of your commands:
mssqlag-primary-deployment-77b8974bb9-dbltl
But kubectl ... --output jsonpath does not print a new line in terminal while kubectl -o json | jq -r prints new line.
So, if you assign the output of your commands to a variable, there will be the same string.
OUTPUT1=$(kubectl get pods -l app=mssql-primary --output jsonpath='{.items[0].metadata.name}')
OUTPUT2=$(kubectl get pods -l app=mssql-primary -o json | jq -r '.items[0].metadata.name')
The $OUTPUT1 is equal to $OUTPUT2.
And you got % just because of the specifics of your shell/terminal.
CodePudding user response:
I'm guessing zsh is your shell. The % is an indicator output by your shell to say that the last line output by kubectl had no newline character at the end. So it's not extra output, it's actually an indicator that the raw kubectl command outputs less than jq.
You could explicitly add a newline to the jsonpath output if you want it:
kubectl get pods -l app=mssql-primary --output jsonpath='{.items[0].metadata.name}{"\n"}'
Or in the other direction you could tell jq not to add newlines at all by specifying -j instead of -r:
kubectl get pods -l app=mssql-primary -o json | jq -j '.items[0].metadata.name'
