Search code examples
kubernetesjqjsonpath

Why is the same jsonpath query returning a different output?


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 ?


Solution

  • 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.