title: How to generate a json object as key, value using postgres?
body: <p>I am trying to extract some statistical data from my database. I have an <code>_employees</code> table and I want to get employee count by country. Suppose <code>_employees</code> table has a column <code>country</code> and I have this query for a key-value representation of an aggregate count.</p>
  
  <pre><code>select json_object_agg(key, value) 
  from (
          select distinct e.country 
          from _employees as e group by (e.country)
      ) as key, 
      (
          select distinct count(1) 
          from _employees as e 
          group by(e.country)
      ) as value;
  </code></pre>
  
  <p>I want the data like this</p>
  
  <pre><code>{
       "ET": 100,
       "JM": 245 // And it will go on like this
  }
  </code></pre>
  
  <p>but I've got the following error </p>
  
  <blockquote>
    <p>key value must be scalar, not array, composite, or json. </p>
  </blockquote>
  
  <p>Can this be done? Is there a better way to go about this?</p>