从Postgres中的2个聚合列构建JSON

时间:2022-06-27 16:11:38

using a Postgres-Db as source for json-documents, I need to convert two columns from a table to an JSON-object.

使用Postgres-Db作为json文档的源代码,我需要将两列从表转换为JSON对象。

So I have the columns "color_id", "language" and "name" in a table of colors:

所以我在颜色表中有“color_id”,“language”和“name”列:

    color_id |   language  | name
    1        |      "de"   |  "blau"
    1        |      "en"   |  "blue"
    1        |      "fr"   |  "bleu"

And I'd like to produce a JSON-object like:

我想生成一个JSON对象,如:

    {
      "de": "blau",
      "fr": "bleu",
      "en": "blue"
    }

I started with

我开始了

    SELECT
      array_to_json(array_agg((language::text, name::text))),
      color_id
    FROM colors
    GROUP BY color_id;

which unfortunately produced

不幸的是产生了

    array to json                 | color_id
    "[{"f1":"de","f2":"blau"}     |
      , {"f1":"en","f2":"blue"}   | 1
      , {"f1":"fr","f2":"bleu"}]" | 

I'd think it would be simple - more or less -, but found myself at a dead end of misleading results and syntax errors.

我认为这很简单 - 或多或少 - 但发现自己处于误导性结果和语法错误的死胡同。

Kind regards, Dominik

亲切的问候,多米尼克

2 个解决方案

#1


6  

Use jsonb_object_agg():

with data(color_id, language, name) as (
values
    (1, 'de', 'blau'),
    (1, 'en', 'blue'),
    (1, 'fr', 'bleu')
)
select color_id, jsonb_object_agg(language, name)
from data
group by 1;

 color_id |              jsonb_object_agg              
----------+--------------------------------------------
        1 | {"de": "blau", "en": "blue", "fr": "bleu"}
(1 row)

The function jsonb_object_agg() was introduced in Postgres 9.5.

函数jsonb_object_agg()是在Postgres 9.5中引入的。

In Postgres 9.4 use json_object_agg() instead.

在Postgres 9.4中使用json_object_agg()代替。

In Postgres 9.3 you have to construct the result using string functions:

在Postgres 9.3中,您必须使用字符串函数构造结果:

select 
    color_id, 
    format('{%s}', string_agg(format('"%s": "%s"', language, name), ', '))::json
from data
group by 1;

#2


0  

For key value pair and Proper JSON data this can be used

对于键值对和Proper JSON数据,可以使用它

with data(color_id, language, name) as (
values
    (1, 'de', 'blau'),
    (1, 'en', 'blue'),
    (1, 'fr', 'bleu')
)
    SELECT color_id,
     json_agg((SELECT x FROM (SELECT language, name) AS x)) AS json_data
FROM data
group by "data".color_id;

#1


6  

Use jsonb_object_agg():

with data(color_id, language, name) as (
values
    (1, 'de', 'blau'),
    (1, 'en', 'blue'),
    (1, 'fr', 'bleu')
)
select color_id, jsonb_object_agg(language, name)
from data
group by 1;

 color_id |              jsonb_object_agg              
----------+--------------------------------------------
        1 | {"de": "blau", "en": "blue", "fr": "bleu"}
(1 row)

The function jsonb_object_agg() was introduced in Postgres 9.5.

函数jsonb_object_agg()是在Postgres 9.5中引入的。

In Postgres 9.4 use json_object_agg() instead.

在Postgres 9.4中使用json_object_agg()代替。

In Postgres 9.3 you have to construct the result using string functions:

在Postgres 9.3中,您必须使用字符串函数构造结果:

select 
    color_id, 
    format('{%s}', string_agg(format('"%s": "%s"', language, name), ', '))::json
from data
group by 1;

#2


0  

For key value pair and Proper JSON data this can be used

对于键值对和Proper JSON数据,可以使用它

with data(color_id, language, name) as (
values
    (1, 'de', 'blau'),
    (1, 'en', 'blue'),
    (1, 'fr', 'bleu')
)
    SELECT color_id,
     json_agg((SELECT x FROM (SELECT language, name) AS x)) AS json_data
FROM data
group by "data".color_id;