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 ArticleAnswer by rishabhr0y for PostgreSQL return result set as JSON array?
use this on your potgres terminal this will return all outputs in json format\pset format json
View ArticleAnswer by Ciro Santilli OurBigBook.com for PostgreSQL return result set as...
Getting just the actual JSON as output with psql -qAtXYou will also be interested in the options mentioned at: How to remove eol (plus sign) character in postgres batch output if you want to get just...
View ArticleAnswer by yymmyb for PostgreSQL return result set as JSON array?
This was copied from the answer to another question.There's another function called json_object_agg, which can generate an object instead of an array.It can be used like this:select...
View Article