I have json stored [1,2,4,5].I have array s like [1,23,4]. I want to check either value of s array exist in json fields. Other options I may need to store array like ['1','2','4','5'] so i can use?| operator or their is anything else i can do ?
-
can you add the table structure, sample data and expected output to understand better.Vivek– Vivek2018-07-17 09:20:04 +00:00Commented Jul 17, 2018 at 9:20
Add a comment
|
1 Answer
There is no ready-to-use function or operator to accomplish that. You can use the function:
create or replace function jsonb_int_array(jsonb)
returns integer[] language sql immutable as $function$
select array(select jsonb_array_elements_text($1)::int)
$function$;
In the query use the overlap operator:
with my_table(data) as (
values ('[1,2,4,5]'::jsonb)
)
select *
from my_table
where jsonb_int_array(data) && array[1,23,4]
data
--------------
[1, 2, 4, 5]
(1 row)