PostgreSQL return result set as JSON array?

前端 未结 2 1915
情书的邮戳
情书的邮戳 2020-11-30 16:41

I would like to have PostgreSQL return the result of a query as one JSON array. Given

create table t (a int primary key, b text);

insert into t values (1, \         


        
2条回答
  •  旧巷少年郎
    2020-11-30 17:26

    Also if you want selected field from table and aggregated then as array .

    SELECT json_agg(json_build_object('data_a',a,
                                      'data_b',b,
    ))  from t;
    

    The result will come .

     [{'data_a':1,'data_b':'value1'}
      {'data_a':2,'data_b':'value2'}]
    

提交回复
热议问题