0

I have a 'notifications' table with a column 'dest_user_id_arr' as json with the following value:

id dest_user_id_arr 1 {"users":[83,84,85]} 2 {"users":[89,83,92]} 3 {"users":[87,88,83]} 

I would like to select those id where user is 83.

1
  • Please tag your PostgreSQL version. Is it a JSON or a JSONB column? Commented Sep 18, 2019 at 13:44

2 Answers 2

0

If the type is jsonb, Use the jsonb containment operator:

SELECT * FROM notifications n WHERE dest_user_id_arr @> '{"users":[83]}'; 

If the type is json or text, either change it to jsonb, or (if you don't care about performance) dynamically cast it.

SELECT * FROM notifications n WHERE dest_user_id_arr::jsonb @> '{"users":[83]}'; 
0

IF column is json:

 SELECT id FROM notifications n WHERE json_array_elements(n.dest_user_id_arr->'users') = 83; 

If column is jsonb:

 SELECT id FROM notifications n WHERE jsonb_array_elements(n.dest_user_id_arr->'users') = 83; 
1
  • "ERROR: set-returning functions are not allowed in WHERE" Commented Sep 18, 2019 at 17:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.