35

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.

  • 1
    I cannot find where "It specifically states you may call JSON_ARRAY_LENGTH passing either a json or jsonb data-type" - could you cite it literally?. – klin Oct 9 '15 at 15:31
  • Must be a misunderstanding. The manual says: json_array_length(json) jsonb_array_length(jsonb). – Erwin Brandstetter Oct 9 '15 at 15:35
  • as @klin says, I think you are misreading the documentation, it shows that you need to call either json_ or jsonb_ versions of the functions depending upon datatype – Doon Oct 9 '15 at 15:35
  • 1
    WOW you're right. I mis-read the documentation, I didn't notice the difference between the names of the functions themselves. – Joshua Burns Oct 9 '15 at 15:43
54
0
SELECT jsonb_array_length('["question","solved"]') AS length;

or

SELECT json_array_length('["question","solved"]') AS length;
| improve this answer | |
  • For me, the function name had to be caps for it to work (windows, perhaps?) JSONB_ARRAY_LENGTH/JSON_ARRAY_LENGTH – Dakusan May 18 '17 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.