JQ select by attribute value

Use jq select() to filter an array of JSON objects by an attribute value. Interactive example that keeps only objects whose field matches a given value.

The jq expression

.servers[] | select(.role == "backend") | .ip

The select(condition) filter passes through only the inputs for which condition is true and drops everything else. Combined with .[] to iterate over an array of objects, it becomes jq's equivalent of a WHERE clause. In this example we iterate over .servers[], keep only the entries where .role == "backend", and then read the .ip of the matching server.