sql - How to get data from postgresql json array field -


i have table field jsonb type , having below data.

{"roles": ["7", "73", "163"]} 

i have check "73" present or not postgresql.

i have search gives solution object of object not object of array.

i have tried below query not work

select *  table  field->'roles' ? array ['73']; 

--updated--

also need record have exact value

{"roles": ["7"]}

{"roles": ["7", "73", "163"]}

i.e. field have "7" not else in it.

by documentation https://www.postgresql.org/docs/current/static/functions-json.html#functions-jsonb-op-table cases:

does –single– key exists in json array:

select  *     table    field -> 'roles' ? '73'; 

does -any- of keys @ right exists in json array:

select  *     table    field -> 'roles' ?| array[ '7', '163' ] ; 

does -all- of keys @ right exists in left json array:

select  *     table    field -> 'roles' ?& array[ '7', '163' ] ; 

does left json array match -exactly- right json array:

select  *     table    field -> 'roles' = $$[ "7" ]$$::jsonb ; 

hopefully helps :)


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -