title: json_agg Two columns in Postgres
body: <p>I have a table which has the following contains two columns that look like the following:</p>
  
  <pre><code>rev | level
  -----------
  B   | 1001
  B   | 1002
  B   | 1003
  C   | 1004
  C   | 1005
  D   | 1006
  </code></pre>
  
  <p>I am trying to return a column that looks like this:</p>
  
  <pre><code>{"B":["1001","1002","1003"], "C":["1002","1003"], "D":["1006"]}
  </code></pre>
  
  <p>the best I could get was using this query:</p>
  
  <pre><code>SELECT d.rev,
         to_json(ARRAY(SELECT level
                       FROM details
                       WHERE rev = d.rev
                       GROUP BY level
                       ORDER BY level DESC
                      )) AS level
  FROM details d
  GROUP BY d.rev
  ORDER BY d.rev DESC
  </code></pre>
  
  <p>This gives me the following rows :</p>
  
  <pre><code>____________________________________
  |  B    | ["1001","1002","1003"]   |
  |  C    | ["1004","1005"]          |
  |  D    | ["1006"]                 |
  |__________________________________|
  </code></pre>
  
  <p>How do I combine these columns into one JSON object?</p>