我想创建一个postgres jsonb数组:[{"score": 4500, "match": 45}, {"score": 2505, "match": 467}, {"score": 967, "match": 678}]
我和我有个别元素{"score": 4500, "match": 45}, {"score": 2505, "match": 467}
等。
我不想将其分解为键值对,然后构建一个jsonb对象。在postgres 9.3中有没有办法?
答案 0 :(得分:0)
jsonb
是introduced in Postgres 9.4。在Postgres 9.3.x中,您只能使用json
:
WITH json_elements(data) AS ( VALUES
('{"score": 4500, "match": 45}'::JSON),
('{"score": 2505, "match": 467}'::JSON)
)
SELECT array_to_json(array_agg(je.data)) AS result
FROM json_elements je;
结果是:
result
--------------------------------------------------------------
[{"match": 45, "score": 4500},{"match": 467, "score": 2505}]
(1 row)