BlindChicken
BlindChicken

Reputation: 71

Postgres query to get array from inside one of the properties of a JSON object

So basically the column(col1) looks like this:

{"property1": [1, 2], "property2": 3}

So I want to get that array of property1 so that I can check if another number is in that array.

The following is what I want to do:

SELECT *  
FROM table  
WHERE 1 = ANY((col1 ->> 'property1'));

Or something to that effect. The problem is that query brings it back as text, and no matter how I try, it does not want to become an array.

Even tried stuff like this:

SELECT *  
FROM table  
WHERE 1 = ANY((col1 -> 'property1')::int[]);//Says jsonb cant be cast to integer

I tried multiple other things and couldn't get it working.

Upvotes: 0

Views: 126

Answers (1)

Kev
Kev

Reputation: 16321

SELECT *
FROM table
WHERE '1'::jsonb <@ (col1->'property1')

Upvotes: 1

Related Questions