Diagnostic Queries
Symptoms
A string being cast to json/jsonb was not valid JSON. PostgreSQL raises SQLSTATE 22032 (invalid_json_text).
- The text doesn’t parse as JSON.
- Common with single quotes, trailing commas, or unquoted keys.
- A DETAIL line points to the parse problem.
What the server log shows
ERROR: invalid input syntax for type json
DETAIL: Token "'" is invalid.
CONTEXT: JSON data, line 1: {'name'...
Why PostgreSQL raises this — what the manual says
Section 8.14 JSON Types:
“JSON data types are for storing JSON (JavaScript Object Notation) data, as specified in RFC 7159.”
JSON/JSONB input is parsed against the JSON grammar. Text that violates it (single quotes, unquoted keys, trailing commas, bad tokens) can’t be parsed, so PostgreSQL reports 22032 with a DETAIL.
Common causes
- Single quotes instead of double quotes for strings/keys.
- Trailing commas or unquoted object keys.
- Truncated or concatenated JSON.
How to fix it
- Produce strict JSON (double quotes, no trailing commas).
- Validate/serialize JSON with a proper library before inserting.
- Read the DETAIL/CONTEXT to locate the offending token.
Related & next steps
Reference: PostgreSQL 18 Section 8.14 “JSON Types”.
Thanks — noted. This helps keep the database accurate.