如何从 JSON 填充嵌套复合类型并插入 table
how to populate nested composite type from JSON and insert into a table
我正在尝试将复杂的 JSON 转换为复合类型。这是一个例子
CREATE TYPE ty as(a int, b int[]);
CREATE TABLE ta(ab ty[]);
INSERT INTO ta(ab) values(ARRAY[ROW(1, ARRAY[1, 1])::ty, ROW(2, ARRAY[2, 2])::ty]);
select * from ta;
ab
-----------------------------------
{"(1,\"{1,1}\")","(2,\"{2,2}\")"}
(1 row)
这很好用。
现在我尝试将 JSON 数组插入到 table 中,方法是先将其填充到复合类型中,然后再将其插入。 PostgreSQL 函数抛出一个奇怪的错误。
INSERT INTO ta(ab) values (json_populate_recordset(null::ty[], '[{"a":3,"b":[3,33]},{"a":4,"b":[4,44]}]'));
ERROR: first argument of json_populate_recordset must be a row type
INSERT INTO ta(ab) values (json_populate_recordset(null::ty, '[{"a":3,"b":[3,33]},{"a":4,"b":[4,44]}]'));
ERROR: column "ab" is of type ty[] but expression is of type ty
LINE 1: INSERT INTO ta(ab) values (json_populate_recordset(null::ty,...
^
HINT: You will need to rewrite or cast the expression.
这只是我分享的一个示例,实际的 JSON 是从其他几个函数生成的。所以我需要修复 JSON 然后将其作为复合类型插入到 table 中。工作非常接近但没有被插入的是:-
INSERT INTO ta(ab) values (json_populate_recordset(null::ty, '[{"a":3,"b":"{3,33}"},{"a":4,"b":"{4,44}"}]'));
ERROR: column "ab" is of type ty[] but expression is of type ty
HINT: You will need to rewrite or cast the expression.
请注意我必须如何将数组转换为 PostgreSQL 喜欢的 JSON 兼容数组,但这仍然不起作用,因为存在类型转换问题。
所以,我真的很想在这里解决两个问题:-
如何将 JSON 转换为 json_populate_recordset 喜欢的兼容 JSON,即复杂的 JSON 被序列化为一个类型(ty in我们的案例)。
如何解决尝试转换和插入类型的 ARRAY 时的类型转换问题。
从 jsonb_to_recordset
构建类型和聚合
insert into ta (ab)
select
array_agg((
a,
(select array_agg(e::int) from jsonb_array_elements_text(b) jae(e))
)::ty)
from jsonb_to_recordset(
'[{"a":3,"b":[3,33]},{"a":4,"b":[4,44]}]'
) as v(a int, b jsonb)
我正在尝试将复杂的 JSON 转换为复合类型。这是一个例子
CREATE TYPE ty as(a int, b int[]);
CREATE TABLE ta(ab ty[]);
INSERT INTO ta(ab) values(ARRAY[ROW(1, ARRAY[1, 1])::ty, ROW(2, ARRAY[2, 2])::ty]);
select * from ta;
ab
-----------------------------------
{"(1,\"{1,1}\")","(2,\"{2,2}\")"}
(1 row)
这很好用。
现在我尝试将 JSON 数组插入到 table 中,方法是先将其填充到复合类型中,然后再将其插入。 PostgreSQL 函数抛出一个奇怪的错误。
INSERT INTO ta(ab) values (json_populate_recordset(null::ty[], '[{"a":3,"b":[3,33]},{"a":4,"b":[4,44]}]'));
ERROR: first argument of json_populate_recordset must be a row type
INSERT INTO ta(ab) values (json_populate_recordset(null::ty, '[{"a":3,"b":[3,33]},{"a":4,"b":[4,44]}]'));
ERROR: column "ab" is of type ty[] but expression is of type ty
LINE 1: INSERT INTO ta(ab) values (json_populate_recordset(null::ty,...
^
HINT: You will need to rewrite or cast the expression.
这只是我分享的一个示例,实际的 JSON 是从其他几个函数生成的。所以我需要修复 JSON 然后将其作为复合类型插入到 table 中。工作非常接近但没有被插入的是:-
INSERT INTO ta(ab) values (json_populate_recordset(null::ty, '[{"a":3,"b":"{3,33}"},{"a":4,"b":"{4,44}"}]'));
ERROR: column "ab" is of type ty[] but expression is of type ty
HINT: You will need to rewrite or cast the expression.
请注意我必须如何将数组转换为 PostgreSQL 喜欢的 JSON 兼容数组,但这仍然不起作用,因为存在类型转换问题。
所以,我真的很想在这里解决两个问题:-
如何将 JSON 转换为 json_populate_recordset 喜欢的兼容 JSON,即复杂的 JSON 被序列化为一个类型(ty in我们的案例)。
如何解决尝试转换和插入类型的 ARRAY 时的类型转换问题。
从 jsonb_to_recordset
insert into ta (ab)
select
array_agg((
a,
(select array_agg(e::int) from jsonb_array_elements_text(b) jae(e))
)::ty)
from jsonb_to_recordset(
'[{"a":3,"b":[3,33]},{"a":4,"b":[4,44]}]'
) as v(a int, b jsonb)