postgres 查找 json 路径是否存在
postgres find whether json path exists
我想使用 psql 查询查找 jsonb 值中是否存在特定路径。
例如,对于这条路径:{"333":"opc":["1333"]}
这个值应该return true:
'{"333":{"opc":[{"1333":"3787"}]}}'
但这些值应该 return false:
'{"333":{"opc":[{"104":"3787"}]}}'
'{"54":{"opc":[{"1333":"3787"},{"1334":"37"}]}}'
'{"333":{"opc":[]}}'
我已经使用 @>
运算符尝试了一些变体,但无法完全获得正确的语法。
例如:
select
'{"333":{"opc":[{"1333":"3787"},{"1334":"37"}]}}'::jsonb @>
'{"333":{"opc":[{"1333"}]}}'::jsonb
这给我一个无效的语法错误
怎么样
select
case when
(select e->'1333' from json_array_elements(data->'333'->'opc') e) is not null
then true
else false
end as status
from t
;
我想使用 psql 查询查找 jsonb 值中是否存在特定路径。
例如,对于这条路径:{"333":"opc":["1333"]}
这个值应该return true:
'{"333":{"opc":[{"1333":"3787"}]}}'
但这些值应该 return false:
'{"333":{"opc":[{"104":"3787"}]}}'
'{"54":{"opc":[{"1333":"3787"},{"1334":"37"}]}}'
'{"333":{"opc":[]}}'
我已经使用 @>
运算符尝试了一些变体,但无法完全获得正确的语法。
例如:
select
'{"333":{"opc":[{"1333":"3787"},{"1334":"37"}]}}'::jsonb @>
'{"333":{"opc":[{"1333"}]}}'::jsonb
这给我一个无效的语法错误
怎么样
select
case when
(select e->'1333' from json_array_elements(data->'333'->'opc') e) is not null
then true
else false
end as status
from t
;