我有两个像这样的 text[] 列:
Col1:
{itema, itemb, itemc}
Col2
{valuea, valueb, valuec}
如何将这两个数组合并到一个 json 中?像这样:
{"itema":"valuea", "itemb":"valueb", "itemc":"valuec"}
最佳答案
使用unnest
将数组转换为行。
select unnest(col1) as col1, unnest(col2) as col2
from test;
col1 | col2
-------+--------
itema | valuea
itemb | valueb
itemc | valuec
然后在 jsonb_object_agg
的子查询中使用它将行聚合在一起作为键/值对。
select jsonb_object_agg(col1, col2)
from (
select unnest(col1) as col1, unnest(col2) as col2
from test
) t;
jsonb_object_agg
----------------------------------------------------------------
{ "itema" : "valuea", "itemb" : "valueb", "itemc" : "valuec" }
关于json - Postgres - 将两个数组合并成一个 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57262124/