【问题标题】:Postgres jsonb get by dynamic pathPostgres jsonb 通过动态路径获取
【发布时间】:2017-01-14 23:34:07
【问题描述】:

是否可以在 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$$

错误:运算符不存在:jsonb #> 文本
没有运算符与给定名称和参数类型匹配。您可能需要添加显式类型转换。

【问题讨论】:

  • '{'||v_key||'}'::jsonb

标签: postgresql jsonb


【解决方案1】:

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';

【讨论】:

    猜你喜欢
    • 2018-08-02
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-29
    • 1970-01-01
    相关资源
    最近更新 更多