title: postgresql function - how to push new json object to json array?
body: <p>I need to push new json object to existing json.</p>
  
  <pre><code>response json ='{"success":[{"aaa":"bbb"}]}'::json;
  newitem json ='{"ccc":"ddd"}'::json;
  </code></pre>
  
  <p>Final response json should like below</p>
  
  <pre><code>{"success":[{"aaa":"bbb"},{"ccc":"ddd"}]}
  </code></pre>
  
  <p>Here is the complete code:</p>
  
  <pre><code>DROP FUNCTION orgname.testfunction();
  CREATE OR REPLACE FUNCTION orgname.testfunction()
  RETURNS json
      LANGUAGE 'plpgsql'
      VOLATILE
  AS $RESPONSE$
  DECLARE
      response json ='{"success":[]}'::json;
      newitem json ='{"ccc":"ddd"}'::json;  
  BEGIN 
      with c(response,newitem) as (values('{"success":[{"aaa":"bbb"}]}'::json,'{"ccc":"ddd"}'::json))
  , m as (select json_array_elements(response-&gt;'success') from c union all select newitem from c)
  select concat('{"success":',json_agg(json_array_elements),'}')::json from m;
      return '{"expecting":"result"}'::json;
  END;
  $RESPONSE$
  </code></pre>