Error happens using jsonpath in oc command

Solution Verified - Updated -

Environment

  • Azure Red Hat OpenShift 4 (ARO)
  • Red Hat OpenShift Container Platform (RHOCP)
    • 4.11

Issue

  • Got unrecognized character error when using && or || regex filter
$ oc get clusterversion -o jsonpath='{.items[].status.conditions[?(@.type=="Upgradeable" && @.status=="False")].message}'

error: error parsing jsonpath {.items[].status.conditions[?(@.type=="Upgradeable" &&  @.status=="False")].message}, unrecognized character in action: U+0026 '&'
$ oc get clusterversion -o jsonpath='{.items[].status.conditions[?(@.type=="Upgradeable" || @.status=="False")].message}'
error: error parsing jsonpath {.items[].status.conditions[?(@.type=="Upgradeable" || @.status=="False")].message}, unrecognized character in action: U+007C '|'

Resolution

Using jq command can help to retrieve related information using and , or command

1, For 'and' condition

$ oc get clusterversion version -o json | jq -r '.status.conditions | .[] | select((.type == "Upgradeable") and (.status == "False")) | .message'

2, For 'or' condition

$ oc get clusterversion version -o json | jq -r '.status.conditions | .[] | select((.type == "Upgradeable") or (.status == "False")) | .message'

Root Cause

Some of the regex filter is not supported in kubectl, and such a command will results in the unrecognized character in action error

The oc binary offers the same capabilities as the kubectl binary towards jsonpath part which will also get error like unrecognized character in action: U+0026 '&'

Diagnostic Steps

Reproducible by running related command

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments