【发布时间】:2022-02-18 23:02:06
【问题描述】:
如何将integer[] 转换为jsonb?
declare ids int[];
declare jsonids jsonb;
jsonids := array(select id from student); -- what should I do here?
【问题讨论】:
标签: sql postgresql plpgsql jsonb
如何将integer[] 转换为jsonb?
declare ids int[];
declare jsonids jsonb;
jsonids := array(select id from student); -- what should I do here?
【问题讨论】:
标签: sql postgresql plpgsql jsonb
使用to_jsonb():
jsonids := to_jsonb(ARRAY(SELECT id FROM student));
或者:
SELECT INTO jsonids to_jsonb(ARRAY(SELECT id FROM student));
array_to_json() 仅用于在json 中获取换行符(而不是jsonb!)。 The manual:
将 SQL 数组转换为 JSON 数组。行为与
to_json除了将在顶级数组之间添加换行符 如果可选的布尔参数为 true,则为元素。
见:
转换回来:
【讨论】:
你可以试试这个
select array_to_json(array_agg(id)) into jsonids from student
【讨论】: