Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Postgresql – How to SELECT using a json aggregate function that takes two columns as key: value pair?

Supposing I have something like

Name username
Mike BestMike99
Someone DaBest

What query do I need to run in order to SELECT/return a json like

json_agg
{Mike: BestMike99, Someone: Dabest}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Having

create table users ("Name" varchar, username varchar);

It would be

select array_to_json(array_agg( concat('{"',"Name",'": "',username,'"}')::json)) from users;

Working sample here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading