0

I have a column with json/jsonb data. Database is postgresql.

column data sample

{"postId": "p522", "groupId": "g11", ...}
{"postId": "p5ds", "groupId": "g234", ...}
{"postId": "p5cz", "groupId": "g597", ...}

I am trying to search as an array of data from json/jsonb column.

What's best or right way to do it?

SELECT * FROM column_name
WHERE 
-- How i can do, similar to this? 
data #> '{groupId}' IN ('g11', 'g597')

-- this works but only for single. I am trying to find by array. 
meta @> '{"groupId": "g597"}' 

2 Answers 2

2

You could use operator ?| if you want something similar to IN. It works with jsonb type.

SELECT * FROM column_name
WHERE meta -> 'groupId' ?| array['g11', 'g597']

More info about json/jsonb operators

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

Comments

0

Solved:

SELECT * FROM column_name 
WHERE meta ->> 'groupId' IN ('g11', 'g597')

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.