1

Currently my query looks like follows and returns the results below:

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

The result looks like this:

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

How do I modify the query above to return all key pair values within the same JSON object (rather then a list of jsons)? Expected output:

61  {"in31" : "0.0", "in32" : "145.8", "in34" : "134.0"}
1
  • 1
    I think you want json_object_agg () Commented Mar 13, 2018 at 14:36

1 Answer 1

3

Replace

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

with

json_object_agg(ds.statement_ref, value) as financials
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Got lost in all the new functions Postgres introduced for jsons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.