22039
PostgreSQLERRORNotableData ExceptionHIGH confidence

SQL/JSON array not found

What this means

SQLSTATE 22039 is raised when a SQL/JSON operation expects a JSON array but the path expression resolves to a non-array value.

Why it happens
  1. 1Applying an array-specific SQL/JSON operation to an object, string, number, or boolean JSON value
How to reproduce

Array SQL/JSON operation on a non-array value.

trigger — this will error
trigger — this will error
SELECT jsonb_path_query('{"a":1}'::jsonb, 'strict $.a[0]');

expected output

ERROR:  jsonpath array subscript is not implemented for type object

Fix

Verify the JSON value is an array before subscripting

WHEN When the JSON structure is not guaranteed to be an array.

Verify the JSON value is an array before subscripting
SELECT jsonb_typeof('{"a":1}'::jsonb -> 'a'); -- check type first

Why this works

Use jsonb_typeof() to confirm the value is an array before applying array subscripts.

Version notes
Postgres 14+

SQL/JSON strict array access refined in Postgres 14.

Sources
Official documentation ↗

Class 22 — Data Exception

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors