Postgres jsonb 通过动态路径获取
Postgres jsonb get by dynamic path
是否可以在 Postgres 中做这样的事情:
do $$
declare
v_key text;
v_json jsonb;
begin
v_key := 'id';
v_json := jsonb_build_object(
'id', jsonb_build_object('nest_id',1)
);
raise notice '%', v_json #> '{'||v_key||'}'->>'nest_id';
end$$
ERROR: operator does not exist: jsonb #> text
No operator matches the given name and argument type(s). You might need to add explicit type casts.
the operator #>
的右操作数类型为文本数组。使用 array[...]
表示法:
raise notice '%', v_json #> array[v_key] ->> 'nest_id';
或显式转换格式化数组文字:
raise notice '%', v_json #> ('{'||v_key||'}')::text[] ->> 'nest_id';
-- or nicer
raise notice '%', v_json #> format('{%s}', v_key)::text[] ->> 'nest_id';
是否可以在 Postgres 中做这样的事情:
do $$
declare
v_key text;
v_json jsonb;
begin
v_key := 'id';
v_json := jsonb_build_object(
'id', jsonb_build_object('nest_id',1)
);
raise notice '%', v_json #> '{'||v_key||'}'->>'nest_id';
end$$
ERROR: operator does not exist: jsonb #> text
No operator matches the given name and argument type(s). You might need to add explicit type casts.
the operator #>
的右操作数类型为文本数组。使用 array[...]
表示法:
raise notice '%', v_json #> array[v_key] ->> 'nest_id';
或显式转换格式化数组文字:
raise notice '%', v_json #> ('{'||v_key||'}')::text[] ->> 'nest_id';
-- or nicer
raise notice '%', v_json #> format('{%s}', v_key)::text[] ->> 'nest_id';