1

I am trying to generate a single json by querying from a table with id as key and email as value like this

{1: "[email protected]", 2: "[email protected]"}

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: "[email protected]"}, {3625: "[email protected]"}]
4
  • 3
    I think you are looking for json_object_agg not json_agg
    – user1822
    Aug 23, 2017 at 11:03
  • @BalasubramanianRaju you probably also want jsonb, not not json Aug 23, 2017 at 13:05
  • Not sure if this is exactly what you're looking for, but you could try my answer here - be careful of the caveats after the answer itself - esp. with respect to \ (backslashes).
    – Vérace
    Aug 23, 2017 at 18:15
  • @a_horse_with_no_name Thanks, I was looking for json_object_agg only and now its working. Aug 23, 2017 at 18:20

1 Answer 1

1

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

Your Answer

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

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