Calculate JSONB Array Length Using PostgreSQL 9.4

Joshua Burns picture Joshua Burns · Oct 9, 2015 · Viewed 25.7k times · Source

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_LENGTH function as described in the PostgreSQL 9.4 Documentation

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

Am I missing something very obvious in the documentation? It specifically states you may call JSON_ARRAY_LENGTH passing either a json or jsonb data-type. I'm explicitly casting to jsonb so I'm at a bit of a loss.

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.

Answer

Jorge André Pereira picture Jorge André Pereira · Nov 25, 2015
SELECT jsonb_array_length('["question","solved"]') AS length;

or

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