How to work with postgresql JSON where keys are arbitrary?

I have a JSON field c1 in table t1 that has the form

{
 "11111" : { "STATUS" : "1"},
 "22222" : { "STATUS" : "0"},
 "33333" : { "STATUS" : "0"}
}

I want to find the line t1 in which any key status is 1, I tried to execute the command

with r1 as(select t1.*, json_object_keys(c1) as keys from t1) select * from r1 where r1.c1->keys->>'STATUS' = '1'; 

but it does not give me a single line?

+4
source share
1 answer
select *
from t1
where exists (
    select 1
    from json_each(c1)
    where value ->> 'STATUS' = '1'
)
+2
source

Source: https://habr.com/ru/post/1526473/


All Articles