Answer by Eugene Chernyavsky for PostgreSQL return result set as JSON array?
For me worksWITH sq AS( -- YOUR QUERY HERE ⬇ SELECT * FROM "foo" WHERE "id"='bar' -- YOUR QUERY HERE ⬆)SELECT json_agg(row_to_json(sq)) FROM sq
View ArticleAnswer by Himanshu sharma for PostgreSQL return result set as JSON array?
Also if you want selected fields from the table and aggregate them as an array:SELECT json_agg(json_build_object('data_a',a,'data_b',b,)) from t;The result will look like this:...
View ArticleAnswer by jpmc26 for PostgreSQL return result set as JSON array?
TL;DRSELECT json_agg(t) FROM tfor a JSON array of objects, and SELECT json_build_object('a', json_agg(t.a),'b', json_agg(t.b) )FROM tfor a JSON object of arrays.List of objectsThis section describes...
View ArticlePostgreSQL return result set as JSON array?
I would like to have PostgreSQL return the result of a query as one JSON array. Givencreate table t (a int primary key, b text);insert into t values (1, 'value1');insert into t values (2,...
View ArticleAnswer by mag for PostgreSQL return result set as JSON array?
A query similar to this worked for me in PostgreSQL 16:SELECT JSON_AGG(r) FROM ( SELECT column1 [AS alias1] , column2 [AS alias2] ,..., columnN [AS alias] FROM table_name WHERE conditions) r
View Article