<p>I have kube service running with 2 named ports like this:</p>

<pre><code>$ kubectl get service elasticsearch --output json
{
    "apiVersion": "v1",
    "kind": "Service",
    "metadata": {
        ... stuff that really has nothing to do with my question ...
    },
    "spec": {
        "clusterIP": "10.0.0.174",
        "ports": [
             {
                "name": "http",
                "nodePort": 31041,
                "port": 9200,
                "protocol": "TCP",
                "targetPort": 9200
            },
            {
                "name": "transport",
                "nodePort": 31987,
                "port": 9300,
                "protocol": "TCP",
                "targetPort": 9300
            }
        ],
        "selector": {
            "component": "elasticsearch"
        },
        "sessionAffinity": "None",
        "type": "NodePort"
    },
    "status": {
        "loadBalancer": {}
    }
}
</code></pre>

<p>I'm trying to get output containing just the 'http' port:</p>

<pre><code>$ kubectl get service elasticsearch --output jsonpath={.spec.ports[*].nodePort}
31041 31987
</code></pre>

<p>Except when I add the test expression as hinted in the cheatsheet here <a href="http://kubernetes.io/docs/user-guide/kubectl-cheatsheet/" rel="nofollow noreferrer">http://kubernetes.io/docs/user-guide/kubectl-cheatsheet/</a> for the name I get an error</p>

<pre><code>$ kubectl get service elasticsearch --output jsonpath={.spec.ports[?(@.name=="http")].nodePort}
-bash: syntax error near unexpected token `('
</code></pre>

<p><code>(</code> and <code>)</code> mean something in bash (see <a href="http://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html" rel="noreferrer">subshell</a>), so your shell interpreter is doing that first and getting confused.  Wrap the argument to <code>jsonpath</code> in single quotes, that will fix it:</p>

<pre><code>$ kubectl get service elasticsearch --output jsonpath='{.spec.ports[?(@.name=="http")].nodePort}'
</code></pre>

<p>For example:</p>

<pre><code># This won't work:
$ kubectl get service kubernetes --output jsonpath={.spec.ports[?(@.name=="https")].targetPort}
-bash: syntax error near unexpected token `('

# ... but this will:
$ kubectl get service kubernetes --output jsonpath='{.spec.ports[?(@.name=="https")].targetPort}'
443
</code></pre>
