Pushing or appending to JSON array in PostgreSQL 9.4
In pre-9.5 you can use json_array_elements
, and array_to_json(array_agg())
, like this.
SELECT array_to_json(array_agg(x))
FROM (
SELECT jsonb_array_elements('[1,2,3]'::jsonb)
UNION ALL SELECT '4'::jsonb
) AS t(x);
You can also write this using the ARRAY
constructor like this..
SELECT array_to_json(ARRAY(
SELECT jsonb_array_elements('[1,2,3]'::jsonb)
UNION ALL SELECT '4'::jsonb
));