栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

合并PostgreSQL中的JSONB值?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

合并PostgreSQL中的JSONB值?

您应该合并

jsonb_each()
两个值都使用的未嵌套元素。在不平凡的查询中执行此操作可能会感到不舒服,因此我更喜欢这样的自定义函数:

create or replace function jsonb_my_merge(a jsonb, b jsonb)returns jsonb language sql as $$    select         jsonb_object_agg( coalesce(ka, kb),  case      when va isnull then vb      when vb isnull then va      else va || vb  end        )    from jsonb_each(a) e1(ka, va)    full join jsonb_each(b) e2(kb, vb) on ka = kb$$;

用:

select jsonb_my_merge(    '{"a":{"b":2}, "d": {"e": 10}, "x": 1}'::jsonb,     '{"a":{"c":3}, "d": {"f": 11}, "y": 2}'::jsonb)    jsonb_my_merge    ------------------------------------------------------------------ {"a": {"b": 2, "c": 3}, "d": {"e": 10, "f": 11}, "x": 1, "y": 2}(1 row)

您可以使用递归稍微修改函数,以使解决方案可以在任何嵌套级别上使用:

create or replace function jsonb_recursive_merge(a jsonb, b jsonb)returns jsonb language sql as $$    select         jsonb_object_agg( coalesce(ka, kb),  case      when va isnull then vb      when vb isnull then va      when jsonb_typeof(va) <> 'object' then va || vb     else jsonb_recursive_merge(va, vb) end        )    from jsonb_each(a) e1(ka, va)    full join jsonb_each(b) e2(kb, vb) on ka = kb$$;

例子:

select jsonb_recursive_merge(     '{"a":{"b":{"c":3},"x":5}}'::jsonb,     '{"a":{"b":{"d":4},"y":6}}'::jsonb);  jsonb_recursive_merge   ------------------------------------------------ {"a": {"b": {"c": 3, "d": 4}, "x": 5, "y": 6}}(1 row)select jsonb_recursive_merge(    '{"a":{"b":{"c":{"d":{"e":1}}}}}'::jsonb,     '{"a":{"b":{"c":{"d":{"f":2}}}}}'::jsonb) jsonb_recursive_merge  ---------------------------------------------- {"a": {"b": {"c": {"d": {"e": 1, "f": 2}}}}}(1 row)

最后,OP建议的功能变化形式(请参阅以下注释):

create or replace function jsonb_recursive_merge(a jsonb, b jsonb) returns jsonb language sql as $$ select     jsonb_object_agg(        coalesce(ka, kb),         case  when va isnull then vb  when vb isnull then va  when jsonb_typeof(va) <> 'object' or jsonb_typeof(vb) <> 'object' then vb  else jsonb_recursive_merge(va, vb) end         )     from jsonb_each(a) e1(ka, va)     full join jsonb_each(b) e2(kb, vb) on ka = kb $$;


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/449234.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号