要映射的数组(名称,值)。
[{'name':'email,'value':'email@email.em'},{'name':'phone','value':123123}]
到
{'email':'email@email.em','phone':123123}
我需要组织搜索和排序此类电子邮件,我想简化以View
使用地图数据结构Table
。
答案 0 :(得分:0)
我将您的输入数据更改为实际上是json(dbl引用而不是单个并添加了数组的键名。此后转换是如何“构建”它的示例:
t=# with j as (
select '{"arr":[{"name":"email","value":"email@email.em"},{"name":"phone","value":123123}]}'::json v
)
select
concat('{',v->'arr'->0->'name',':',v->'arr'->0->'value',',',v->'arr'->1->'name',':',v->'arr'->1->'value','}')::json
from j
;
{"email":"email@email.em","phone":123123}
答案 1 :(得分:0)
with t(id,j) as (
values(1,'[{"name":"email","value":"email@email.em"},{"name":"phone","value":123123}]'::json))
select json_object_agg(a.j->>'name', a.j->>'value')
from t, json_array_elements(j) a(j) group by id;
╔════════════════════════════════════════════════════╗
║ json_object_agg ║
╠════════════════════════════════════════════════════╣
║ { "email" : "email@email.em", "phone" : "123123" } ║
╚════════════════════════════════════════════════════╝