0

I have a json path command to get all the labels of my pods.

kubectl get pods -o jsonpath="{.items[*].metadata.labels}"

This will output:

{
    "app": "api-dogs-v1",
    "release": "0.0.119"
} {
    "app": "api-cats-v1",
    "release": "0.0.16"
} 

I want to do some simple manipulation of the command so that it outputs valid json and surround inside a json object.

I'm trying the below:

kubectl get pods -o jsonpath='{"{"}{{range .items[*]}{.metadata.labels}{"}"}{end}'

But this gives me back:

unrecognized character in action: U+007B '\'

Can anyone help me with this? Desired output below:

{
    {
        "app": "api-dogs-v1",
        "release": "0.0.119"
    } {
        "app": "api-cats-v1",
        "release": "0.0.16"
    } 
}

thanks!

...........................................

Update

kubectl get pods -o jsonpath='"richard"{range .items[*]}{.metadata.labels}{end}"}"'

Almost gets me there but I get an error when i change to add curly braces at the start of the json path expression (instead of my name) i believe it thinks i'm starting the function...

kubectl get pods -o jsonpath='"{"{range .items[*]}{.metadata.labels}{end}"}"'

error: error parsing jsonpath {{range .items[*]}{.metadata.labels}{end}}, unrecognized character in action: U+007B '{'

Richie
  • 4,989
  • 24
  • 90
  • 177
  • I found that you have opened one extra open brace in your command. Kindly recheck the command and try once. – Ramesh kollisetty Nov 17 '21 at 17:12
  • really bizarre. i've also found that it won't respect my "\n" character when i do this kubectl get pods -o jsonpath='{range .items[*]}{.metadata.labels}{"\n"}{end}. Wondering if this has to do with powershell? – Richie Nov 18 '21 at 10:45
  • tried that... so weird... all that does is echo the jsonpath expression back to me – Richie Nov 19 '21 at 05:59
  • On Windows, you must double quote any JSONPath template that contains spaces . This in turn means that you must use a single quote or escaped double quote around any literals in the template. For example: `kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{\"\t\"}{.status.startTime}{\"\n\"}{end}"` https://kubernetes.io/docs/reference/kubectl/jsonpath/ – Akshay G Nov 19 '21 at 06:01
  • Also check this https://stackoverflow.com/a/55602560/4018180 – Akshay G Nov 19 '21 at 06:02

2 Answers2

2

i have remove extra { infront of {range .items[*]} and moved {"}"} after {end} as following :

kubectl get pods -o jsonpath='{"{"}{range .items[*]}{.metadata.labels}{end}{"}"}'

Output(your desired output format): 
{{"app":"test-multi-pv","pod-template-hash":"55665bc94c"}{"app":"nginx","controller-revision-hash":"web1-774fbdcb49","statefulset.kubernetes.io/pod-name":"web1-0"}{"app":"nginx","controller-revision-hash":"web2-6b59d76fc6","statefulset.kubernetes.io/pod-name":"web2-0"}{"app":"nginx","controller-revision-hash":"web3-7c65fbbcdc","statefulset.kubernetes.io/pod-name":"web3-0"}}
confused genius
  • 2,876
  • 2
  • 16
  • 29
  • this didn't work for me....error: error parsing jsonpath {{}{range .items[*]}{.metadata.labels}{end}{}}, unrecognized character in action: U+007B '{' – Richie Nov 18 '21 at 07:23
  • Maybe it's got to do with escaping characters? I'm using powershell if that makes any difference – Richie Nov 18 '21 at 07:23
1

I found that you have opened one extra open brace in your command. Kindly recheck the command and try once.

I have reproduced the use case and successfully got the expected output.while running the following jsonpath command, I have got all the labels of my pods.

kubectl get pods -o jsonpath="{.items[*].metadata.labels}"

Output:

{

"app":"hello-server", "Pod-template-hash":"5bd6b6875f"

}

I have removed the curly braces of the jsonpath expression “{“ before the {range .items[*]} and added a newline expression before the {end}

So, the final range function for the json object that surrounds the inside json object is

kubectl get pods -o jsonpath='{"{"}{range .items[*]}{.metadata.labels}{"}"}{"\n"}{end}'

Expected Output:

{

{

 "app":"hello-server",

 "Pod-template-hash":"5bd6b6875f"

}

}

  • The result I get from the above is .... error: error parsing jsonpath {{}{range .items[*]}{.metadata.labels}{}}{\n}{end}, unrecognized character in action: U+007B '{' – Richie Nov 19 '21 at 00:16
  • Given you have tested this and is works for you I feel like this might be something specific to powershell? – Richie Nov 19 '21 at 00:18