How to query all rows of a table into single json Postgresql? I am trying to generate a single json by querying from a table with id as key and email as value like this

{1: "abc@example.com", 2: "abcd@example.com"}


My attempt to create is:

select json_agg(json_build_object(id, email)) 
from auth_user 
where id in (4733, 3625, 4961, 4237);


which results me 

[{4733: "abc@example.com"}, {3625: "abcd@example.com"}]
Migrating a comment to an answer.


  I think you are looking for json_object_agg not json_agg – a_horse_with_no_name 2 hours ago


Links


JSON Functions and Operators