英文:
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.
答案3
得分: 1
首先修复 val_fld_3
的值,我猜你想要一个下划线
您可以嵌套查询以获取内部数组,并使用聚合和构建数组,如下所示:
注意使用 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
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.
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论