Skip to content
Advertisement

SELECT on JSON operations of Postgres array column?

I have a column of type jsonb[] (a Postgres array of jsonb objects) and I’d like to perform a SELECT on rows where a criteria is met on at least one of the objects. Something like:

I’ve looked around at ANY and UNNEST but it’s not totally clear how to achieve this, since you can’t run unnest in a WHERE clause. I also don’t know how I’d specify that I want the field1 from the matching object.

Do I need a WITH table with the values expanded to join against? And how would I achieve that and keep the id from the other column?

Thanks!

Advertisement

Answer

You need to unnest the array and then you can access each json value

This will return one row for each json value in the array.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement