PeterVandivier
I have a document of the form...
```
{
"a": [
{
"b": {"x": 1},
"c": 2
},
{
"b": {"y": 2},
"d": 3
},
{
"b": {"x": 2},
"d": 4
}
]
}
```
Usinq [`jq`](https://stedolan.github.io/jq/), I would like to retrieve the object(s) from with the array `a[]` that have the exact value 2 for the nested attribute `x`.
In postgresql, it might look something like this:
<>https://dbfiddle.uk/?rdbms=postgres_13&fiddle=1f69408704bd95b38c55660c661529f6
I've been rubber-ducking my way through [this post](https://stackoverflow.com/questions/26701538/how-to-filter-an-array-of-objects-based-on-values-in-an-inner-array-with-jq) but it seems to be more about fuzzy matching text (not what I want) and filtering against an array with a pipe-into-[`contains()`](https://stedolan.github.io/jq/manual/#contains(element)) (vs filter against scalars in an unfolded array).
What syntax do I need to use to use to extract the full object `{"b":{"x":2},"d":4}` from the above document?
Top Answer
PeterVandivier
The [`select()` function](https://stedolan.github.io/jq/manual/#select(boolean_expression)) only ever evaluates a boolean. The bit that's confusing from the array/`contains()` examples is the addition of the pipeline _inside_ the `select()`. For exact equality. this is not necessary as `select()` can probe an arbitrary path into the object against which it evaluates in order complete it's input expression.
In this case, the expression is `.a[] | select(.b.x == 2)'`
::: tio S0oszvj/PzU5I19BnauaS0FBKVHJSiEayFBQqAaTQKEkoFC1UgWQNKzVgQkmA7lGYA5UDE15JUgeoTwFyDXGo7wCU7kJRDmQjOWq5VJXUKhRyCpUUNdLjI4FMotTc1KTSzT0kvQqFGxtFYw01f//BwA
§§§ bash bash
echo '
{
"a": [
{
"b": {"x": 1},
"c": 2
},
{
"b": {"y": 2},
"d": 3
},
{
"b": {"x": 2},
"d": 4
}
]
}
' | jq '.a[] | select(.b.x == 2)'
§§§
``` none
{
"b": {
"x": 2
},
"d": 4
}
```
:::
To compare back to our postgres example...
* `.a[]` corresponds to `jsonb_array_elements('{...}'::jsonb->'a')`
* the `|` pipeline passes the objects from the left side through as individual tuples/rows
* `select()` runs once-per-tuple and evaluates the expression inside `()`
* a [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) evalution returns the whole tuple/object
* `.b.x` is the probe `obj->'b'->'x'`
* `==` [double equals is the necessary syntax](https://www.tutorialfunda.com/js/single-equal-vs-double-equals-vs-triple-equals-in-javascript/).
^HT^ ^to^ [^@jalkjaer^](https://gist.github.com/jalkjaer) ^who's^ ^rubber^ ^ducked^ ^this^ ^before^ ^on^ [^gist^]( https://gist.github.com/ipbastola/2c955d8bf2e96f9b1077b15f995bdae3#gistcomment-2708458)