invalid JSON text
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.
- 1Casting or parsing a string that is not valid JSON to JSON or JSONB type
- 2Single-quoted strings instead of double-quoted JSON strings
- 3Trailing commas, missing commas, or other JSON syntax violations
Casting an invalid JSON string to JSONB.
SELECT '{key: value}'::jsonb; -- unquoted key is invalid JSONexpected output
ERROR: invalid input syntax for type json
Fix 1
Use valid JSON syntax with double-quoted keys and string values
WHEN When constructing JSON strings in SQL or application code.
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 When building JSON from SQL expressions.
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.
Class 22 — Data Exception
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev