How to Fix Postgres Error 22036 Non Numeric SQL Json Item

Published
Length
4 min read

Luis Felipe Dias

All posts

The postgres error 22036 non numeric sql json item happens when a query runs math on text or objects in JSON data. You fix it by checking the JSON value type before running math or by cleaning up bad input records. Database engines enforce strict data types during JSON path runs. When a path expression expects a number, any other JSON type triggers a hard stop. Your app then gets a failed transaction instead of a query result. What causes postgres error 22036 non numeric sql json item In the

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

The postgres error 22036 non numeric sql json item happens when a query runs math on text or objects in JSON data. You fix it by checking the JSON value type before running math or by cleaning up bad input records.

Database engines enforce strict data types during JSON path runs. When a path expression expects a number, any other JSON type triggers a hard stop. Your app then gets a failed transaction instead of a query result.

What causes postgres error 22036 non numeric sql json item

In the official PostgreSQL error list, SQLSTATE 22036 maps to the macro ERRCODE_NON_NUMERIC_SQL_JSON_ITEM. This error belongs to the data exception class of database failures. It fires when a SQL JSON path tries a number operation on a non-number item.

JSON columns often store flexible data from many external apps. One app version might write an age or price field as a plain number. A newer app version might send that same field as a quoted text string. Sometimes an API sends a null value, a boolean flag, or a nested array.

When your query runs an arithmetic operator or a number method inside a JSON path, the engine checks each item. If the engine hits a string or object, it stops the query right away. Even a single bad row in a large table will break the whole read query.

Client apps can also cause this bug when they build dynamic queries. If a user passes a wrong key name into a search filter, the JSON path points to the wrong field. Pointing a math filter at a name or email field triggers error 22036 right away.

How to find the bad JSON records safely

To fix this failure in production, you must find the rows that hold non-number values. Do not give developers full superuser rights just to inspect broken production data. Instead, use least-privilege database roles to run safe read checks.

In PostgreSQL, the SELECT privilege lets a role read from specific columns of a table or view. You can grant a support team read rights on just the primary key and the broken JSON column. You can also set up group roles to manage these read privileges across many users. When an engineer joins the group role, they get only the exact column rights needed to debug the error.

Non-Human Identity Management + JSON Web Tokens (JWT): architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Also check your default role rules before letting engineers run test queries. The special role name PUBLIC grants privileges to every role on the system. By default, PUBLIC gets CONNECT and TEMPORARY on databases, plus EXECUTE on functions and procedures. In PostgreSQL 15 and later, the default setup no longer lets every user create objects in the public schema. On older databases, an admin should revoke CREATE on the public schema from PUBLIC so engineers do not leave test tables behind.

Once the team has safe read rights, they can filter the table by JSON value type. Have them query the type of the target JSON key across all rows. This step shows which rows hold strings, booleans, or objects instead of numbers.

How to fix the query and prevent future errors

You can fix the immediate query crash in two ways. First, you can update the SQL JSON path so it checks the item type before running math. Filtering out non-number items inside the path stops the engine from throwing SQLSTATE 22036.

Second, you can handle the error gracefully inside the JSON query function. Many SQL JSON functions let you define a default behavior when an error occurs. Setting the query to return null on error keeps the read query running past bad rows.

For a permanent fix, clean the stored data and lock down future writes. Run a reviewed update script to convert quoted number strings into true JSON numbers. Then add a table check constraint that checks the JSON field type on every insert and update. If your JSON data holds sensitive fields like emails, phone numbers, card numbers, or national IDs, apply data masking during debug sessions so masking rewrites matched fields in results.

Run guardrails and masking on the Postgres connection

Role grants and firewall rules are needed setup. Setup alone cannot check single SQL statements or mask sensitive rows in a response. By design, hoop.dev sits inline in the Postgres protocol beside Postgres, RDS, or Aurora as a sidecar for your Postgres. On every production query from an engineer or AI agent, hoop.dev decides whether to pass, mask, block, or hold it for approval.

First, hoop.dev guardrails refuse disallowed drops or deletes before they run, and risky statements can pause for human review. Next, hoop.dev data masking rewrites sensitive fields in returned rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and logs every statement and verdict per resource. Check out the project on GitHub.

Frequently asked questions

What does SQLSTATE 22036 mean in PostgreSQL?

SQLSTATE 22036 is the standard error code for ERRCODE_NON_NUMERIC_SQL_JSON_ITEM. It means a SQL JSON path expression tried to apply a number operation or math method to a JSON string, boolean, array, or object.

Does a quoted number in JSON trigger error 22036?

Yes, JSON treats a number inside quotes as a text string rather than a numeric value. When a strict SQL JSON path runs math or number methods on that quoted string, PostgreSQL raises error 22036.

How can engineers debug JSON data errors without full table access?

Admins can grant the SELECT privilege on only the specific JSON column and row ID column. Assigning this limited privilege to a group role lets engineers find bad JSON types without reading other table columns.

The query runs. The emails stay hidden.