使用 PostgreSQL 9.4 计算 JSONB 数组长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33041184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:03:42  来源:igfitidea点击:

Calculate JSONB Array Length Using PostgreSQL 9.4

postgresqlpostgresql-9.4jsonb

提问by Joshua Burns

I'm running the latest version of PostgreSQL 9.4.5-1.pgdg14.04+1, and am attempting to calculate the length of a JSONB array using the JSON_ARRAY_LENGTHfunction as described in the PostgreSQL 9.4 Documentation

我正在运行最新版本的 PostgreSQL 9.4.5-1.pgdg14.04+1,并尝试使用PostgreSQL 9.4 文档中JSON_ARRAY_LENGTH描述的函数计算 JSONB 数组的长度

Here is the exact query I'm attempting to run:

这是我试图运行的确切查询:

SELECT JSON_ARRAY_LENGTH('["hi","ho"]'::jsonb) AS length

When I run that query, I would expect to be returned a value of 2, but instead am encountering the error: ERROR: function json_array_length(jsonb) does not exist

当我运行该查询时,我希望返回的值为2,但我遇到了错误:ERROR: function json_array_length(jsonb) does not exist

Am I missing something very obvious in the documentation? It specifically states you may call JSON_ARRAY_LENGTHpassing either a jsonor jsonbdata-type. I'm explicitly casting to jsonbso I'm at a bit of a loss.

我是否遗漏了文档中非常明显的内容?它特别说明您可以调用JSON_ARRAY_LENGTH传递 ajsonjsonb数据类型。我明确地投射到jsonb所以我有点不知所措。

Has anyone else encountered this problem, or would someone point out what I'm doing wrong here?

有没有其他人遇到过这个问题,或者有人会指出我在这里做错了什么?

UPDATE: I Mis-Read The Documentation

更新:我误读了文档

I should have been calling JSONB_ARRAY_LENGTH, not JSON_ARRAY_LENGTH. Notice the "B" after "JSON". Thanks guys.

我应该打电话JSONB_ARRAY_LENGTH,不是JSON_ARRAY_LENGTH。注意“JSON”后面的“B”。多谢你们。

回答by Jorge André Pereira

SELECT jsonb_array_length('["question","solved"]') AS length;

or

或者

SELECT json_array_length('["question","solved"]') AS length;