The sql_json_array_not_found error (SQLSTATE 22039) means a SQL/JSON path expression expected an array but found a non-array value.
sql_json_array_not_found means PostgreSQL could not locate an array at the specified SQL/JSON path. Cast the target JSON element to an array or correct the path to point at an existing array to resolve the error.
sql_json_array_not_found
The PostgreSQL error sql_json_array_not_found (code 22039) is raised by SQL/JSON functions when a path expression reaches a location that is not an array, even though an array was required by the query.
The error often appears in SELECT lists, WHERE clauses, or UPDATE statements that use jsonb_path_query, jsonb_path_exists, or similar SQL/JSON operators with the strict (#>) array accessor.
Resolving the issue quickly is important because the statement fails completely – no rows are returned or modified until the JSON path is corrected.
PostgreSQL throws 22039 when the query assumes a JSON element is an array but encounters an object, scalar, or missing key.
Any strict array accessor in the path immediately aborts.
Using zero-based index notation (like [0]) against a scalar value or optional array causes the same failure in strict mode.
First, verify the JSON column really contains an array at the targeted path.
Inspect a sample row with jsonb_path_query_first or -> operator.
If the data model allows null or scalar values, switch to lax mode by prefixing the array step with ? or use the optional (#?) operator so non-arrays are skipped instead of raising an error.
Scenario 1 - Selecting the first tag in an events column:
SELECT data#>>'{tags,0}' FROM logs;
This fails when tags is not an array.
Use:
SELECT data#>>'{tags?(@type() == "array"),0}' FROM logs;
Scenario 2 - Updating nested items:
UPDATE orders
SET details = jsonb_set(details,'{items,0,price}', '9.99')
WHERE id = 1;
Ensure items is an array or cast it first.
Validate JSON schemata before ingestion so array fields are always arrays. Apply CHECK constraints using jsonb_typeof().
Use lax path modifiers or COALESCE to default missing arrays to an empty array.
Document JSON contracts in your API layer.
Error 22032 (sql_json_item_cannot_be_array) occurs when you try to create an array where one is not allowed.
Error 22030 (sql_json_scalar_required) indicates a scalar was expected at the path. Fixes mirror those for 22039 - inspect types and correct the path.
.
Using #> or [index] on a path that resolves to an object or string triggers the error.
When the key is absent PostgreSQL treats the result as non-array and raises 22039.
If some rows store the field as an object and others as an array, strict queries fail for the non-array rows.
Prior ETL steps might cast an array to text inadvertently, leaving downstream queries expecting an array.
.
sql_json_array_not_found exists from PostgreSQL 12 when SQL/JSON functions were introduced. Earlier versions will not show this code.
Yes. Use lax path modifiers or optional operators (#?) so PostgreSQL skips rows where the array is missing.
Galaxy's AI copilot inspects sample data and warns when a path expects an array but detects mixed types, proposing lax operators automatically.
Lax evaluation adds negligible overhead compared to a full table scan. Index usage remains unchanged.