PostgreSQL: invalid input syntax for type json
ERROR: invalid input syntax for type jsonA value cast to json or jsonb is not valid JSON, commonly from single quotes, trailing commas, unquoted keys, or double-encoded strings. Serialize proper JSON in your app and bind it as a json parameter.
What this error means
json and jsonb columns require syntactically valid JSON. When the text is malformed, Postgres rejects it, usually with a DETAIL naming the invalid token and its position.
Frequent causes are hand-built JSON strings, single quotes instead of double quotes for keys, or a payload that was truncated or double-encoded.
How to fix it
The JSON is malformed
Serialize it properly in your application using the language JSON encoder, then insert it.
You built JSON by string concatenation
Bind the value as a real json parameter rather than concatenating, so quoting is correct.
INSERT INTO t (data) VALUES ($1::jsonb); -- $1 is a valid JSON stringKeys use single quotes
JSON requires double-quoted keys and strings. Fix the encoding at the source.
How to avoid it next time
- Never build JSON by hand; use your language serializer.
- Bind JSON values as jsonb parameters rather than concatenating strings.
Frequently asked questions
What does the DETAIL 'Token is invalid' mean?
It points at the exact character where JSON parsing failed, for example a trailing comma or a single quote. Fix that spot.
Does json or jsonb matter for this error?
Both require valid JSON, so both raise this on malformed input. jsonb also normalizes and validates structure when stored.
Stop struggling with SQL
Use FluentDB, the AI database client for macOS.
Related errors
Part of the PostgreSQL error reference. Last reviewed 2026-07-31.