Suppose we have two columns: id
, params
.
params
is a jsonb column that contains arrays of objects like:
[
{
"prop": "a",
"answer": "123"
},
{
"prop": "b",
"answer": "456"
}
]
I need to create a query that returns id
+ answer
where prop
has some specific value (so at most one row per id
).
For example, you can filter by prop=a
, and in this case, you'll get 123 for that particular row. Other rows (with other ids) can have a different value or don't have it all (since there could be no prop=a
in their jsons).
I tried some solutions with jsonb_array_elements(params)
but I always ended up having all possible values from the json even if you have prop=a
only in one of the elements (so id
is duplicated in the result set with each answer
).
Also, I obviously cannot use solutions based on ordinality of the elements in the arrays.