how do I use jsonb_array_length with condition?
You need to unnest array elements with jsonb_array_elements():
select id, value
from t
cross join jsonb_array_elements(ss)
where (value->'approved')::bool
Use the boolean aggregate function bool_and
to check whether all elements of an array has "approved": true
select id, bool_and((value->'approved')::bool)
from t
cross join jsonb_array_elements(ss)
group by id
Db<>Fiddle.
Read in the docs:
- JSON Functions and Operators
- Aggregate Functions