Search code examples
kubectl

Need kubectl jsonpath parameter to get array entry indexed by string


I have some scripts that pipe "kubectl ... -o json" to "jq". I've been converting many of those occurrences to use the "jsonpath" parameter to kubectl so I can skip the jq step.

There's one set of these that I've been unable to convert, because of punctuation issues. The original line looks something like this:

kubectl ... -o json | jq -r '.data["application.properties"]'

In the json, there is a "data" map whose keys can have periods in the names, so I have to index it as an array, with the name as the key. This works fine using jq.

The simple-minded conversion of this to jsonpath looks something like this:

kubectl ... -o=jsonpath="{.data["application.properties"]}"

This fails with:

error: error parsing jsonpath {.data[application.properties]}, invalid array index application.properties

This doesn't appear to be a quoting problem, as it is clearly detecting what my attempted array index is, it just won't let me use it.

Is there any way to do this entirely with jsonpath?


Solution

  • The trick is to convert

    {.data["application.properties"]}
    

    To:

    {.data.application\.properties}
    

    (with quoting as required)