返回多个键值对的json,而不是单对键值jsons的列表

时间:2018-03-13 14:31:23

标签: json postgresql jsonb

目前我的查询如下所示,并返回以下结果:

select
    c.id as company_id,
    json_agg(json_build_object(ds.statement_ref, value)) as financials
from
    st.data_statements ds
    join st.company_data cd on ds.company_datum_id = cd.id
    join st.companies c on cd.company_id = c.id
where
    c.id = 61
group by
    c.id

结果如下:

61  [{"in31" : "0.0"}, {"in32" : "145.8"}, {"in34" : "134.0"}]

如何修改上面的查询以返回同一JSON对象中的所有密钥对值(而不是jsons列表)?预期产出:

61  {"in31" : "0.0", "in32" : "145.8", "in34" : "134.0"}

1 个答案:

答案 0 :(得分:1)

替换

json_agg(json_build_object(ds.statement_ref, value)) as financials

json_object_agg(ds.statement_ref, value) as financials