FluentDBFluentDB
PostgreSQL errors

PostgreSQL: invalid input syntax for type json

ERRORSQLSTATE 22P02invalid_text_representation
ERROR: invalid input syntax for type json

A 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

01

The JSON is malformed

Serialize it properly in your application using the language JSON encoder, then insert it.

02

You built JSON by string concatenation

Bind the value as a real json parameter rather than concatenating, so quoting is correct.

sql
INSERT INTO t (data) VALUES ($1::jsonb);  -- $1 is a valid JSON string
03

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