title: Return multiple columns of the same row as JSON array of objects
body: <p>I have the following table <code>MyTable</code>:</p>
  
  <pre><code> id │ value_two │ value_three │ value_four 
  ────┼───────────┼─────────────┼────────────
    1 │ a         │ A           │ AA
    2 │ a         │ A2          │ AA2
    3 │ b         │ A3          │ AA3
    4 │ a         │ A4          │ AA4
    5 │ b         │ A5          │ AA5
  </code></pre>
  
  <p>I want to query an array of objects <code>{ value_three, value_four }</code> grouped by <code>value_two</code>. <code>value_two</code> should be present on its own in the result. The result should look like this:</p>
  
  <pre><code> value_two │                                                                                    value_four                                                                                 
  ───────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   a         │ [{"value_three":"A","value_four":"AA"}, {"value_three":"A2","value_four":"AA2"}, {"value_three":"A4","value_four":"AA4"}]
   b         │ [{"value_three":"A3","value_four":"AA3"}, {"value_three":"A5","value_four":"AA5"}]
  </code></pre>
  
  <p>It does not matter whether it uses <code>json_agg()</code> or <code>array_agg()</code>.</p>
  
  <p>However the best I can do is:</p>
  
  <pre><code>with MyCTE as ( select value_two, value_three, value_four from MyTable ) 
  select value_two, json_agg(row_to_json(MyCTE)) value_four 
  from MyCTE 
  group by value_two;
  </code></pre>
  
  <p>Which returns:</p>
  
  <pre><code> value_two │                                                                                    value_four                                                                                 
  ───────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   a         │ [{"value_two":"a","value_three":"A","value_four":"AA"}, {"value_two":"a","value_three":"A2","value_four":"AA2"}, {"value_two":"a","value_three":"A4","value_four":"AA4"}]
   b         │ [{"value_two":"b","value_three":"A3","value_four":"AA3"}, {"value_two":"b","value_three":"A5","value_four":"AA5"}]
  </code></pre>
  
  <p>With an extra <code>value_two</code> key in the objects, which I would like to get rid of. Which SQL (Postgres) query should I use?</p>