A very simple
echo '{"nodes":[{"id":"1"}]}' | jq '.nodes[?(@.id=="1")]'
fails due to
jq: error: syntax error, unexpected '?' (Unix shell quoting issues?) at <top-level>, line 1:
.nodes[?(@.id=="1")]
jq: 1 compile error
The hint "Unix shell quoting issues?" doesn't apply because everything is '
quoted. However, I tried various unnecessary combinations of simple and double quotes and escaped versions thereof combined with escaping of [
, [
, @
and ?
.
echo '{"nodes":[{"id":"1"}]}' | jq '.nodes'
works as does echo '{"nodes":[{"id":"1"}]}' | jq '.nodes[0]'
, so the error must be in the filter expression which is very simple, seen like this in dozens of examples and which I'm using in integration tests with the Jayway JsonPath library.
I'm using jq 1.6 on Ubuntu 21.04.
The filter, '.nodes[?(@.id=="1")]'
is in JsonPath syntax.
How to make the simplest jq filter query work?
Change it to a JQ select like so:
jq '.nodes[] | select(.id == "1")'
Where
.nodes[]
loops over all the nodesselect(.id == "1")
only keep the objects were id === 1