【问题标题】:How to compose a postgres jsonb array from individual elements如何从单个元素组成一个 postgres jsonb 数组
【发布时间】:2015-12-23 15:52:19
【问题描述】:

我想创建一个 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 有办法吗?

【问题讨论】:

  • 你有他们“和你在一起”是什么意思?它们是否存储在 jsonb 列中,每行一个元素或其他什么?请澄清。

标签: postgresql postgresql-9.3 jsonb


【解决方案1】:

jsonbintroduced 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)

【讨论】:

    猜你喜欢
    • 2015-07-12
    • 2021-01-08
    • 2021-05-12
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 2011-03-31
    相关资源
    最近更新 更多