skip to Main Content

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}

2

Answers


  1. 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

    Login or Signup to reply.
  2. You can use jsonb_object_agg()

    select jsonb_object_agg(name, username)
    from the_table;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search