Calculate JSONB Array Length Using PostgreSQL 9.4

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

or

SELECT json_array_length('["question","solved"]') AS length;
Share:
44,329
Joshua Burns
Author by

Joshua Burns

Online Presence LinkedIn: http://www.linkedin.com/in/joshuadburns Stack Overflow: http://stackoverflow.com/users/253254/joshua-burns Where I Spend My Time Founder, Partner - Initial Holdings LLC, Wip My Ride, TPMS Authority, Hordes Of Hobbies, Curb Hoppers --- https://tpmsauthority.com Board Member, Treasurer - The Champion Project, a Non-Profit Organization: https://thechampionproject.co Background 20+ yrs. experience in programming and technology. 12 yrs. in eCommerce. 10 yrs. Project Management. Knowledge/Experience Python, PHP, Javascript, Perl PostgreSQL, MSSQL, SQLite, MongoDB, Redis, Solr, Memcached, Varnish, HA Proxy Response, Aloha, Pro ERP

Updated on May 29, 2020

Comments

  • Joshua Burns
    Joshua Burns almost 4 years

    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.