将记录转换为在Postgres 14中所需的JSON格式。

huangapple go评论65阅读模式
英文:

converting records to the desired json format in postgres 14

问题

我有以下形式的数据

select t.name_field, t.value_field 
from 
(
	values 
		('name_1', 'val_fld_1'), 
		('name_2', null), 
		('name_3', 'val_fld__3')
) as t(name_field,value_field);

从这个数据中,我需要获得以下形式的JSON字符串:

{"name_1" : [{"value" : "val_fld_1", "seq" : 1}], "name_2" : [{"value" : "", "seq" : 1}], "name_3" : [{"value" : "val_fld__3", "seq" : 1}]}

我尝试这样做:

select array_agg(json_build_object(t.name_field, json_build_array(json_build_object('value',  t.value_field)))) as my_test
from 
(
	values 
		('name_1', 'val_fld_1'), 
		('name_2', null), 
		('name_3', 'val_fld__3')
) as t(name_field,value_field);

但它没有给我期望的结果。
如何实现期望的JSON格式?

英文:

I have data in the following form

select t.name_field, t.value_field 
from 
(
	values 
		('name_1', 'val_fld_1'), 
		('name_2', null), 
		('name_3', 'val_fld__3')
) as t(name_field,value_field);

From this data, I need to get a json string of the following form:

{"name_1" : [{"value" : "val_fld_1", "seq" : 1}], "name_2" : [{"value" : "", "seq" : 1}], "name_3" : [{"value" : "val_fld__3", "seq" : 1}]}

I am trying to do it like this:

select array_agg(json_build_object(t.name_field, json_build_array(json_build_object('value',  t.value_field)))) as my_test
from 
(
	values 
		('name_1', 'val_fld_1'), 
		('name_2', null), 
		('name_3', 'val_fld__3')
) as t(name_field,value_field);

but it doesn't give me the desired result.
How do I achieve the desired json format?

答案1

得分: 1

SELECT json_object_agg(t.name_field, json_build_array(json_build_object('value', t.value_field, 'seq', 1))) AS my_test
FROM
(
VALUES
('name_1', 'val_fld_1'),
('name_2', null),
('name_3', 'val_fld__3')
) AS t(name_field, value_field);

更多关于 JSON 函数的信息,请参考:https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSON-CREATION-TABLE

英文:

I guess it should work:

SELECT json_object_agg(t.name_field, json_build_array(json_build_object('value', t.value_field, 'seq', 1))) AS my_test
FROM 
(
    VALUES 
        ('name_1', 'val_fld_1'), 
        ('name_2', null), 
        ('name_3', 'val_fld__3')
) AS t(name_field, value_field);

more about json functions:
https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSON-CREATION-TABLE

答案2

得分: 1

只需执行以下操作:

select  ('{'||
string_agg(concat('"',t.name_field,'" : [{"value" : "',t.value_field,'", "seq" : 1}]'), ',')
    ||'}')::json
from 
(
    values 
        ('name_1', 'val_fld_1'), 
        ('name_2', null), 
        ('name_3', 'val_fld__3')
) as t(name_field,value_field);

以按预期组装您的 JSON。

英文:

Just do

select  ('{'||
string_agg(concat('"',t.name_field,'" : [{"value" : "',t.value_field,'", "seq" : 1}]'), ',')
	||'}')::json
from 
(
    values 
        ('name_1', 'val_fld_1'), 
        ('name_2', null), 
        ('name_3', 'val_fld__3')
) as t(name_field,value_field);

to assemble your JSON as expected.

Working sample here

答案3

得分: 1

首先修复 val_fld_3 的值,我猜你想要一个下划线 将记录转换为在Postgres 14中所需的JSON格式。

您可以嵌套查询以获取内部数组,并使用聚合和构建数组,如下所示:

注意使用 Coalesce 返回空字符串而不是 null。

可用的示例链接

SELECT json_object_agg(t.name_field,
json_build_array(json_build_object('value', coalesce(t.value_field,''), 'seq', 1))) AS res
FROM 
(
    VALUES 
        ('name_1', 'val_fld_1'), ('name_2', null),('name_3', 'val_fld_3')
) AS t(name_field, value_field);
英文:

first fix the val_fld_3 value i guess you wanted one underscore 将记录转换为在Postgres 14中所需的JSON格式。

You can nest queries to get the inner array, and using aggregate and build array as:

Notice the Coalesce to return empty string instead of null.

Fiddle available here

SELECT json_object_agg(t.name_field,
json_build_array(json_build_object('value', coalesce(t.value_field,''), 'seq', 1))) AS res
FROM 
(
    VALUES 
        ('name_1', 'val_fld_1'), ('name_2', null),('name_3', 'val_fld_3')
) AS t(name_field, value_field);

huangapple
  • 本文由 发表于 2023年7月7日 02:18:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定