Looks my question is unusual because I didn't find the answer at all. Let's imagine, I have table A with columns: language and uri.

language | uri             
ru       | some-uri        
en       | some-another-uri

My question is: How can I return JSON object instead of many rows. E.g.:

{ "ru": "some-uri", "en": "some-another-uri", ... }

  • 1
    Please tag your DBMS (version included) – McNets May 6 '18 at 13:25
  • @McNets it is about PostgreSQL – Stanislav Tarasenko May 6 '18 at 20:35

So, I found out the answer far far away in the documentation.

SELECT json_object(array_agg(language), array_agg(uri)) FROM A will give you the expected result.

  • You will need to cast the fields (like language and uri) to ::text in case they are not text – Ranjeet Oct 8 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.