22032
PostgreSQLERRORNotableData ExceptionHIGH confidence

invalid JSON text

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
trigger — this will error
SELECT '{key: value}'::jsonb; -- unquoted key is invalid JSON

expected 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.

Use valid JSON syntax with double-quoted keys and string values
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.

Use jsonb_build_object() to construct JSON safely in SQL
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 documentation ↗

Class 22 — Data Exception

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors