Postgres - 将行数据作为JSON数组或数组返回

时间:2016-09-12 18:16:38

标签: sql arrays json postgresql postgresql-9.4

在PG v9.4及更高版本上,我想将一些数据(基于SELECT语句)导出为阵列的JSON数组

标准的json_agg返回我想要的东西,但它返回对象数组(其中对象键是列名) 例如,此查询:

SELECT json_agg(data_rows)
FROM (
        -- in memory table sample
    SELECT * FROM 
    (VALUES
        ('John',now(),1940,'Winston','Lennon'),
        ('Paul',now(),1942,'','McCartney'),
        ('George',now(),1943,NULL,'Harrison'),
        ('Ringo',now(),1940,'my passions are ring,drum and shades','Starr')
    ) AS x("FirstName", "CurrentDt", "BirthYear", "MiddleName", "LastName")
        ORDER BY "BirthYear" DESC, "FirstName" DESC
) AS data_rows

返回以下内容:

[
 {"FirstName":"George","CurrentDt":"2016-09-12T13:13:07.862485-04:00","BirthYear":1943,"MiddleName":null,"LastName":"Harrison"}, 
 {"FirstName":"Paul","CurrentDt":"2016-09-12T13:13:07.862485-04:00","BirthYear":1942,"MiddleName":"","LastName":"McCartney"}, 
 {"FirstName":"Ringo","CurrentDt":"2016-09-12T13:13:07.862485-04:00","BirthYear":1940,"MiddleName":"my passions are ring,drum and shades","LastName":"Starr"}, 
 {"FirstName":"John","CurrentDt":"2016-09-12T13:13:07.862485-04:00","BirthYear":1940,"MiddleName":"Winston","LastName":"Lennon"}
]

但我想要的是:

[
 ["George","2016-09-12T13:13:07.862485-04:00",1943,null,"Harrison"}, 
 ["Paul","2016-09-12T13:13:07.862485-04:00",1942,"","McCartney"}, 
 ["Ringo","2016-09-12T13:13:07.862485-04:00",1940,"my passions are ring,drum and shades","Starr"}, 
 ["John","2016-09-12T13:13:07.862485-04:00",1940,"Winston","Lennon"}
]

我曾尝试使用技巧mentioned here首先将行转换为hstore,但问题是不保留列排序.... 所以这个查询:

SELECT json_agg(avals(hstore(data_rows)))
FROM (
        -- in memory table sample
    SELECT * FROM 
    (VALUES
        ('John',  now(),1940,'Winston','Lennon'),
        ('Paul',  now(),1942,'','McCartney'),
        ('George',now(),1943,NULL,'Harrison'),
        ('Ringo', now(),1940,'my passions are ring,drum and shades','Starr')
    ) AS x("FirstName", "CurrentDt", "BirthYear", "MiddleName", "LastName")
        ORDER BY "BirthYear" DESC, "FirstName" DESC
) AS data_rows
-- placing order by 'outside' did not make any difference
--ORDER BY "BirthYear" DESC, "FirstName" DESC

返回以下(列排序错误)

[
 ["Harrison","1943","2016-09-12 14:07:06.772227-04","George",null], 
 ["McCartney","1942","2016-09-12 14:07:06.772227-04","Paul",""], 
 ["Starr","1940","2016-09-12 14:07:06.772227-04","Ringo","my passions are ring,drum and shades"], 
 ["Lennon","1940","2016-09-12 14:07:06.772227-04","John","Winston"]
]

有谁知道如何将结果作为数组的JSON数组?

1 个答案:

答案 0 :(得分:3)

你可能想要这样的东西:

encoding

我使用SELECT json_agg(info) FROM ( SELECT json_build_array("LastName","BirthYear","CurrentDt","FirstName","MiddleName") AS info FROM (VALUES ('John',now(),1940,'Winston','Lennon'), ('Paul',now(),1942,'','McCartney'), ('George',now(),1943,NULL,'Harrison'), ('Ringo',now(),1940,'my passions are ring,drum and shades','Starr') ) AS x("FirstName", "CurrentDt", "BirthYear", "MiddleName", "LastName") ORDER BY "BirthYear" DESC, "FirstName" DESC ) as t; 将每个人的所有值放在json数组中,然后在外部查询中,我使用json_build_array将所有这些数组收集到一个数组数组中。

您还可以在aggragate函数中移动json_agg子句以获取以下内容:

ORDER BY