1
{
  "List": [
    {
      "f1": "eed31964",
      "f2": "ABC"
    },
    {
      "f1": "964433d1",
      "f2": "DEF"
    }
  ]
}

JSON list contains 2 objects. How to filter where

f1 IN ('eed31964','964433d1') AND f2 IN ('ABC','DEF').

I have tried

column->'List' @> '[{"f1": "eed31964"},{"f1": "964433d1"}]'

but this works like AND condition where as IN operator works like OR operator.

3
  • Does this answer your question? Select "WHERE IN" with PostgreSQL and JSONB Commented Nov 22, 2022 at 11:46
  • No, I am asking about List of objects not the single object. Commented Nov 22, 2022 at 12:00
  • See if this works for you. Commented Nov 22, 2022 at 14:08

1 Answer 1

0

You can apply successive cross joins to access the list elements, and then apply your filtering conditions:

select jsonb_object_agg(t1.key, t1.l) from (
   select t2.key, jsonb_agg(v.value) l 
   from tbl t cross join jsonb_each(t.js) t2 
   cross join jsonb_array_elements(t2.value) v 
   where v.value ->> 'f1' in ('eed31964','964433d1') 
       and v.value ->> 'f2' in ('ABC','DEF') 
   group by t2.key) t1

See fiddle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.