0

How do I get the query below to return 'true' to reflect that I need at least one of the value in the array on the right, to be contained in the array on the left?

SELECT   '["fun", "movies"]'::jsonb  @> '["fun", "movies","x"]'::jsonb;

The above returns false (because 'x' is not in the array on the left).

I have tried the ?| operator link

SELECT   '["fun", "movies"]'::jsonb  ?| '["fun", "movies","x"]'::jsonb;

but postgres returns an error

ERROR: operator does not exist: jsonb ?| jsonb LINE 2: SELECT
'["fun", "movies"]'::jsonb ?| '["fun", "movies","x...

HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. SQL state: 42883 Character: 147

1 Answer 1

1

you need to "convert" jsonb array to postgres array first:

t=# SELECT   '["fun", "movies"]'::jsonb  ?| translate('["fun", "movies","x"]'::jsonb::text,'[]','{}')::text[];
 ?column?
----------
 t
(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.