Postgres 9.5 - 查询嵌套JSON元素的数组长度

时间:2016-12-11 11:52:00

标签: json postgresql postgresql-9.5

我有一个Postgres表,其中包含一个JSON字段,用于标识与给定记录关联的图像。该字段的内容如下所示:

 {"photo-verification": 
   {"photos": [
     {"type": "photo-verification", "fileName": "4f35a880-e9a0-43f9-a31e-1bb8b765d04d", "mimeType": "image/jpeg", "createdBy": "jmlittm", "createdTs": "2016-06-20T20:25:39.706Z", "delFlag": false, "updatedBy": "jmlittm", "updatedTs": "2016-06-20T20:25:39.706Z"},
     {"type": "photo-verification", "fileName": "3a104d07-dc48-4f59-b83f-06cd35a21dae", "mimeType": "image/jpeg", "createdBy": "jmlittm", "createdTs": "2016-06-20T22:31:09.808Z", "delFlag": false, "updatedBy": "jmlittm", "updatedTs": "2016-06-20T22:31:09.808Z"}
     ]
   }
}

记录可以包含0个或更多相关图像 - 如果没有图像,则整个字段将为空。我正在尝试编写一个查询来确定与给定记录关联的图像数量。最终结果应该是有多少记录有一个图像的计数,以及有多少记录有多个记录。如果我查询JSON的顶级,如下所示:

select n.images->'photo-verification' from notes n;

我可以获得内部JSON,其中包含照片数组,但如果我尝试深入挖掘,我就得不到任何结果。我的想法是我可以按照

的方式做点什么

select array_length(n.images->'photo-verification'->'photos', 1) from notes n;

select json_array_length(n.images->'photo-verification'->'photos') from notes n;

但我最终得到的错误和暗示可能我应该考虑投射。

我刚开始潜入Postgres,所以我仍然试图绕过查询语言的一些细节。我会继续研究,但有人可能提供的任何帮助或见解将不胜感激。

编辑:

所以,我认为我可以通过创建一个只有“照片”的视图来简化问题。 JSON并过滤掉所有空字段:

CREATE VIEW photos as SELECT n.images->'photo-verification' as photo FROM notes.notes n where (n.images->'photo-verification')::text != '';

它有效,因为我现在有一个带有JSON列的视图,如下所示:

   {"photos": [
     {"type": "photo-verification", "fileName": "4f35a880-e9a0-43f9-a31e-1bb8b765d04d", "mimeType": "image/jpeg", "createdBy": "jmlittm", "createdTs": "2016-06-20T20:25:39.706Z", "delFlag": false, "updatedBy": "jmlittm", "updatedTs": "2016-06-20T20:25:39.706Z"},
     {"type": "photo-verification", "fileName": "3a104d07-dc48-4f59-b83f-06cd35a21dae", "mimeType": "image/jpeg", "createdBy": "jmlittm", "createdTs": "2016-06-20T22:31:09.808Z", "delFlag": false, "updatedBy": "jmlittm", "updatedTs": "2016-06-20T22:31:09.808Z"}
     ]
   }

但是,如果我尝试

select json_array_length(photo) from photos;

我明白了:

ERROR: cannot get array length of a scalar

如果我尝试

select json_array_length(photo->'photos') from photos;

我收到一堆空白记录。

我必须在这里遗漏一些东西......

2 个答案:

答案 0 :(得分:3)

似乎需要转换为JSON:

select json_array_length((images->'photo-verification'->'photos')::json)
from notes;

答案 1 :(得分:2)

create temp table t (id int, data json);
insert into t values 
(1, 
'{"photo-verification": 
   {"photos": [
     {"type": "photo-verification", "fileName": "4f35a880-e9a0-43f9-a31e-1bb8b765d04d", "mimeType": "image/jpeg", "createdBy": "jmlittm", "createdTs": "2016-06-20T20:25:39.706Z", "delFlag": false, "updatedBy": "jmlittm", "updatedTs": "2016-06-20T20:25:39.706Z"},
     {"type": "photo-verification", "fileName": "3a104d07-dc48-4f59-b83f-06cd35a21dae", "mimeType": "image/jpeg", "createdBy": "jmlittm", "createdTs": "2016-06-20T22:31:09.808Z", "delFlag": false, "updatedBy": "jmlittm", "updatedTs": "2016-06-20T22:31:09.808Z"}
     ]
   }
}'),
(2, 
'{"photo-verification": 
   {"photos": [
     {"type": "photo-verification", "fileName": "4f35a880-e9a0-43f9-a31e-1bb8b765d04d", "mimeType": "image/jpeg", "createdBy": "jmlittm", "createdTs": "2016-06-20T20:25:39.706Z", "delFlag": false, "updatedBy": "jmlittm", "updatedTs": "2016-06-20T20:25:39.706Z"}
     ]
   }
}')
;

select id, json_array_length(data->'photo-verification'->'photos')
from t;

或者,如果您对某些特定领域感兴趣:

select json_array_elements(data->'photo-verification'->'photos')->>'fileName' as fileName
from t;