Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 22036: Non-Numeric SQL JSON Item

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Error 22036 means PostgreSQL tried to convert a JSON value to a number and the value was not numeric. The fix is to find the offending row with a safe cast, clean or filter the data, and then change the query so it validates before converting. You do not need to restart the server or reinstall anything: the error is about data, not the database engine.

What You'll Need

  • Access to a PostgreSQL client such as psql, pgAdmin, or another SQL client.
  • Permission to read the table and, if you will correct data, permission to update it.
  • The name of the table and the JSON or JSONB column involved.
  • A backup or at least a tested copy of the rows you plan to change.

Step-by-Step

  1. Read the full error message. PostgreSQL usually includes the failing value or a fragment of it. Write down the table, column, and value it mentions. If the message is truncated, check your client's message log or the server log for the complete text.
  2. Identify the query that fails. Look for a cast such as ::numeric, ::int, or a function like CAST(... AS numeric) applied to a JSON field. Typical forms are (data->>'price')::numeric or (data->'qty')::int. The ->> operator returns text, and casting that text to a number is where 22036 is raised.
  3. Find the rows with non-numeric values before casting. Run a query that returns the raw text and flags anything that does not look like a number. A practical check is:
    SELECT id, data->>'price' AS raw FROM your_table WHERE data->>'price' IS NOT NULL AND data->>'price' !~ '^[0-9]+(\.[0-9]+)?$';
    Adjust the column and key names. This returns empty strings, words, currency symbols, commas, and nulls that are stored as the text null.
  4. Inspect the bad values. Look for common causes: empty string '', the text 'null', a value with a thousands separator like '1,200', a currency symbol like '$19.99', a unit like '12 kg', or a JSON number stored as a string with spaces. Each needs a different correction.
  5. Decide whether to clean the data or guard the query. If the column should always hold numbers, correct the rows. If the column legitimately mixes types, change the query to skip or default non-numeric values instead of casting blindly.
  6. Clean the data safely. Update only the rows you identified, and run the update inside a transaction so you can roll back: BEGIN; UPDATE your_table SET data = jsonb_set(data, '{price}', to_jsonb(trim(both from replace(data->>'price', ',', ''))::numeric)) WHERE id IN (...); COMMIT; Test the expression on a SELECT first. For jsonb columns, jsonb_set is the standard way to replace one key. For plain json columns, rewrite the whole document or convert the column to jsonb first.
  7. Guard the query when data cannot be cleaned. Use a regular expression or a safe conversion so a bad row does not abort the whole statement. For example, filter with WHERE data->>'price' ~ '^[0-9]+(\.[0-9]+)?$' before the cast, or use a CASE expression that returns NULL for non-numeric text. In PostgreSQL 16 and later, pg_input_is_valid(text, 'numeric') tests whether a string can be cast without raising an error; on older versions the regular expression check is the portable approach.
  8. Re-run the original query. It should now complete. If it still fails, repeat step 3 against the exact expression in the failing query, because the bad value may be in a different key or a nested path.
  9. Add a constraint or check to prevent recurrence. If the JSON key must be numeric, add a CHECK constraint that validates the extracted text, for example CHECK (data->>'price' ~ '^[0-9]+(\.[0-9]+)?$'). This rejects bad writes at insert time instead of failing later at read time. Test the constraint against existing rows before adding it, because PostgreSQL validates all current rows.

Common Mistakes

  • Casting the JSON value instead of its text. data->'price' returns json or jsonb, and casting that directly to numeric can produce a different error. Use data->>'price' to get text first.
  • Assuming NULL is the problem. A SQL NULL cast to numeric is fine. The error comes from a non-null string that is not a valid number, including the literal text 'null' or an empty string.
  • Forgetting whitespace and separators. ' 42 ' may cast, but '1,200' and '$5' will not. Trim and strip formatting before casting.
  • Updating without a transaction. A mistaken UPDATE on a JSON column can damage many rows. Always wrap bulk changes in BEGIN and COMMIT, and verify with a SELECT first.
  • Fixing one key and missing nested ones. If the query reads data->'order'->>'total', the bad value is in a nested object, not the top level.
  • Adding a constraint before cleaning. The constraint will fail to create if existing rows violate it. Clean first, then add it.

Tips

  • Run the detection query against a copy of the table or in a read-only session first, so you see the scope of the problem before changing anything.
  • Keep the raw text in a separate column if the source data is unreliable; store the cleaned numeric value in a typed column and let the application validate input.
  • Prefer jsonb over json for new columns: it supports indexing and the jsonb_set function, which makes corrections simpler.
  • Use EXPLAIN on the corrected query to confirm the filter is applied before the cast, not after.
  • Log the failing value in your application when the error occurs, so the next occurrence is easier to trace.

Troubleshooting

If the error persists after cleaning, check whether the failing query reads a different key or a view that joins several tables. Run the detection query against each source. If the error appears only in one application, compare the SQL it sends with the SQL you tested; an ORM may add its own cast. If you cannot change the data, wrap the cast in a CASE expression or filter with a regular expression. If the error appears during an import, validate the input file before loading and load into a staging table first. If you are unsure which row fails, add a temporary column that stores the raw text and cast it in a separate step to isolate the value. Never disable constraints or rewrite data without a backup; take a dump of the affected table first.

Frequently Asked Questions

What does PostgreSQL error 22036 mean exactly?

It is the invalid text representation error raised when a string cannot be converted to the requested numeric type. In JSON context it usually comes from casting a JSON text value to numeric, int, or a similar type.

Does this error mean my database is corrupted?

No. The database is working correctly by rejecting a value that is not a number. The problem is the data or the query, not the server.

Can I stop the error without changing the data?

Yes. Filter non-numeric values with a regular expression or a CASE expression before the cast, or use pg_input_is_valid on PostgreSQL 16 and later.

Why does the error mention a value I cannot find?

The value may be nested inside a JSON object or array, or it may be an empty string or the text 'null'. Search the raw text of the column rather than the parsed value.

Should I store numbers as JSON strings?

No, if the value is always numeric. Store it as a numeric column or as a JSON number, and validate input in the application. JSON strings are appropriate only when the type genuinely varies.

Will adding a CHECK constraint fix existing bad rows?

No. The constraint prevents new bad rows, but PostgreSQL validates existing rows when you add it, so clean the data first or the constraint creation will fail.