jqlang / jq

Command-line JSON processor
https://jqlang.github.io/jq/
Other
30.05k stars 1.55k forks source link

Found strange behavior in a request with []? #1623

Closed tackin closed 6 years ago

tackin commented 6 years ago

I have build a jq-request and found this:

Does not give any error nor prints any value: jq -r '.nodes[] | select(.nodeinfo.network.mesh[].interfaces.tunnel[]? == "0a:2f:f8:76:d7:7a" or .nodeinfo.network.mesh[].interfaces.wireless[]? == "0a:2f:f8:76:d7:7a" or .nodeinfo.network.mesh[].interfaces.other[]? == "0a:2f:f8:76:d7:7a") | .nodeinfo.hostname' /var/www/meshviewer/data/nodes.json

This one works fine and shows me the requested hostname: jq -r '.nodes[] | select( .nodeinfo.network.mesh[].interfaces.wireless[]? == "0a:2f:f8:76:d7:7a" or .nodeinfo.network.mesh[].interfaces.other[]? == "0a:2f:f8:76:d7:7a" or .nodeinfo.network.mesh[].interfaces.tunnel[]? == "0a:2f:f8:76:d7:7a") | .nodeinfo.hostname' /var/www/meshviewer/data/nodes.json

(Just puting the "...tunnel[]?"-part at the end of the select-request makes it working.)

I'm no jq-expert, so if this is no issue, please ignore and close the issue.

pkoppstein commented 6 years ago

if this is no issue, please ignore and close the issue.

Without seeing the JSON it's hard to tell for sure, but it looks like you can close the issue, the point being that postfix ? is like try _ catch empty, that is, to produce an empty stream in case of the kind of error that try is able to catch.

tackin commented 6 years ago

This is the json-file: http://maps.tackin.de/data/nodes.json

pkoppstein commented 6 years ago

The following jq query shows that there is only one path leading to "0a:2f:f8:76:d7:7a":

$ jq -c 'paths as $p | getpath($p) as $v | select($v == "0a:2f:f8:76:d7:7a") | [$p, $v]' nodes.json
[["nodes",496,"nodeinfo","network","mesh","bat0","interfaces","wireless",0],"0a:2f:f8:76:d7:7a"]

Thus for example your first query, that is:

.nodes[] | select(.nodeinfo.network.mesh[].interfaces.tunnel[]? == "0a:2f:f8:76:d7:7a")

should indeed produce the empty stream, because it involves .tunnel.

tackin commented 6 years ago

Thank you for your help and patience!