【问题标题】:How to convert integer[] to jsonb in a PL/pgSQL code block如何在 PL/pgSQL 代码块中将整数 [] 转换为 jsonb
【发布时间】: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


    【解决方案1】:

    使用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,则为元素。

    见:

    转换回来:

    【讨论】:

      【解决方案2】:

      你可以试试这个

      select array_to_json(array_agg(id)) into jsonids from student
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-02
        • 1970-01-01
        • 2017-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多