简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM