PG
PRO
22032ERRORTier 2 — Caution✅ HIGH confidence

invalid JSON text

Category: Data ExceptionVersions: All Postgres versions

What this means

SQLSTATE 22032 is raised when a string passed to a JSON or JSONB function or cast is not valid JSON text. The input must be well-formed JSON.

Why it happens

  1. 1Casting or parsing a string that is not valid JSON to JSON or JSONB type
  2. 2Single-quoted strings instead of double-quoted JSON strings
  3. 3Trailing commas, missing commas, or other JSON syntax violations

How to reproduce

Casting an invalid JSON string to JSONB.

trigger — this will ERROR
SELECT '{key: value}'::jsonb; -- unquoted key is invalid JSON
ERROR: invalid input syntax for type json

Fix 1: Use valid JSON syntax with double-quoted keys and string values

When constructing JSON strings in SQL or application code.

fix
SELECT '{"key": "value"}'::jsonb;

Why this works

JSON requires double-quoted keys and string values, no trailing commas, and proper nesting.

Fix 2: Use jsonb_build_object() to construct JSON safely in SQL

When building JSON from SQL expressions.

fix
SELECT jsonb_build_object('key', 'value', 'count', 42);

Why this works

jsonb_build_object() constructs valid JSONB from SQL arguments without requiring manual JSON string construction.

Sources

📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 22 — Data Exception

Confidence assessment

✅ HIGH confidence

Standard SQLSTATE for JSON parse errors. Stable across all Postgres versions that support JSON.

See also

📄 Reference pages

JSON FunctionsJSONBjsonb_build_object
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →