如何在postgres 9.3中将json数组转换为postgres int数组

Max*_*ddy 8 arrays postgresql json postgresql-9.3

我有一个场景,我需要将一个json数组转换为postgres int数组并查询它的结果.下面是我的阵列

      ID            DATA
       1           {"bookIds" : [1,2,3,5], "storeIds": [2,3]} 
       2           {"bookIds" : [4,5,6,7], "storeIds": [1,3]}
       3           {"bookIds" : [11,12,10,9], "storeIds": [4,3]}
Run Code Online (Sandbox Code Playgroud)

我想将booksId数组转换为int数组,然后查询它.在postgres 9.3中有可能吗?我知道9.4 +提供了更多的JSON支持,但我目前无法更新我的数据库.

下面的查询给我错误

  Select data::json->>'bookIds' :: int[] from table

 ERROR:  malformed array literal: "bookIds"
 LINE 1: Select data::json->>'bookIds' :: int[] from table
Run Code Online (Sandbox Code Playgroud)

是否有可能在postgres 9.3中查询json数组中的元素..提前感谢...

kli*_*lin 12

问题中的设置应如下所示:

create table a_table (id int, data json);
insert into a_table values
(1, '{"bookIds": [1,2,3,5], "storeIds": [2,3]}'), 
(2, '{"bookIds": [4,5,6,7], "storeIds": [1,3]}'),
(3, '{"bookIds": [11,12,10,9], "storeIds": [4,3]}');
Run Code Online (Sandbox Code Playgroud)

请注意json值的正确语法.

你可以使用这个功能 json_array_elements()

select id, array_agg(e::text::int)
from a_table, json_array_elements(data->'bookIds') e
group by 1
order by 1;

 id |  array_agg   
----+--------------
  1 | {1,2,3,5}
  2 | {4,5,6,7}
  3 | {11,12,10,9}
(3 rows)    
Run Code Online (Sandbox Code Playgroud)

使用any()搜索的阵列,例如一个元素:

select *
from (
    select id, array_agg(e::text::int) arr
    from a_table, json_array_elements(data->'bookIds') e
    group by 1
    ) s
where 
    1 = any(arr) or
    11 = any(arr);

 id |     arr      
----+--------------
  1 | {1,2,3,5}
  3 | {11,12,10,9}
(2 rows)
Run Code Online (Sandbox Code Playgroud)

另请阅读<@ operator.

您还可以通过检查其元素来搜索json数组(不将其转换为int数组),例如:

select t.*
from a_table t, json_array_elements(data->'bookIds') e
where e::text::int in (1, 11);

 id |                     data                      
----+-----------------------------------------------
  1 | {"bookIds" : [1,2,3,5], "storeIds": [2,3]}
  3 | {"bookIds" : [11,12,10,9], "storeIds": [4,3]}
(2 rows)
Run Code Online (Sandbox Code Playgroud)


Alf*_*bel 9

就我而言,我必须将存储在表 col 中的 json 数据转换为 pg 数组格式,这很方便:

-- username is the table column, which has values like ["john","pete","kat"]

select id, ARRAY(SELECT json_array_elements_text((username)::json)) usernames
from public.table-name;

-- this produces : {john,pete,kat}
Run Code Online (Sandbox Code Playgroud)


Joe*_*l B 7

这两个函数(for json/ jsonb)从对这个问题的精彩答案修改而来,效果很好

CREATE OR REPLACE FUNCTION json_array_castint(json) RETURNS int[] AS $f$
    SELECT array_agg(x)::int[] || ARRAY[]::int[] FROM json_array_elements_text($1) t(x);
$f$ LANGUAGE sql IMMUTABLE;

CREATE OR REPLACE FUNCTION jsonb_array_castint(jsonb) RETURNS int[] AS $f$
    SELECT array_agg(x)::int[] || ARRAY[]::int[] FROM jsonb_array_elements_text($1) t(x);
$f$ LANGUAGE sql IMMUTABLE;
Run Code Online (Sandbox Code Playgroud)

您可以按如下方式使用它们:

SELECT json_array_castint('[1,2,3]')
Run Code Online (Sandbox Code Playgroud)

这给预期收益{1,2,3}integer[]。如果您想知道为什么我在每个SELECT语句中都连接一个空数组,那是因为转换是有损的并且没有它,如果您尝试将空json/jsonb数组转换为一个,integer[]您将不会得到任何回报(不需要)而不是一个空数组(如预期)。用上面的方法当你做

SELECT json_array_castint('[]')
Run Code Online (Sandbox Code Playgroud)

你会得到{}而不是一无所有。请参阅此处了解有关我添加该内容的原因的更多信息。