1

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 ?

1
  • can you add the table structure, sample data and expected output to understand better. Commented Jul 17, 2018 at 9:20

1 Answer 1

2

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) 
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.