title: How to count setof / number of keys of JSON in postgresql?
body: <p>I have a column in <code>jsonb</code> storing a map, like <code>{'a':1,'b':2,'c':3}</code> where the number of keys is different in each row. </p>
  
  <p>I want to count it --  jsonb_object_keys can retrieve the keys but it is in <code>setof</code></p>
  
  <p>Are there something like this?</p>
  
  <pre><code>(select count(jsonb_object_keys(obj) from XXX )
  </code></pre>
  
  <p>(this won't work as <code>ERROR: set-valued function called in context that cannot accept a set</code>)</p>
  
  <p><a href="http://www.postgresql.org/docs/9.4/static/functions-json.html" rel="noreferrer">Postgres JSON Functions and Operators Document</a></p>
  
  <pre><code>json_object_keys(json)
  jsonb_object_keys(jsonb)
  
  setof text  Returns set of keys in the outermost JSON object.
  json_object_keys('{"f1":"abc","f2":{"f3":"a", "f4":"b"}}')  
  
  json_object_keys
  ------------------
  f1
  f2
  </code></pre>
  
  <p>Crosstab isn't feasible as the number of key could be large.</p>