SQLSTATE 42846Severity lowLab verified

Incident brief

Cannot coerce

A value was cast to a type with no defined conversion path. PostgreSQL refused the cast instead of guessing at one.

What lands in your log

ERROR: cannot cast type json to integer

Reproduced on PostgreSQL 16.14Verified 2026-07-16 (Docker lab, PostgreSQL 16.14)Verified against PostgreSQL 16.14 in an isolated lab environment

In 10 seconds

What triggers it
Cast a json value directly to a type with no defined json cast, such as integer.
Fix
Extract the specific field or value from the json first (with ->> or ->), then cast that extracted text.
Proof
Reproduced on PostgreSQL 16.14 → Casting a json object directly to integer was rejected with SQLSTATE 42846. The session was unaffected afterward.

Fix

What to do right now

Application-level steps for this error.

  • Extract the specific field or value from the json first (with ->> or ->), then cast that extracted text.
  • Don't route the value through ::text as a generic bypass, see the counterexample for how that trades one error for a more confusing one.
  • For scalar-only json values, casting via ::text can appear to work, but it isn't a reliable general pattern.
Fix SQL
SELECT ('{"a":1}'::json->>'a')::integer;

Runbook to fix this

The runbook for this incident

Full step-by-step fixes for the condition behind this error: the diagnosis, the exact SQL, and output captured in the lab.

Connected

Everything this error touches

Every page this SQLSTATE connects to: the concept that explains it, the runbooks that fix it, the parameters you tune to prevent it, and the sibling errors it travels with. All real cross-references. Jump straight in, or open the full interactive map.

Open in the interactive map →

Verification

PG 16.14
Last verified
2026-07-16 (Docker lab, PostgreSQL 16.14)
Verification scope
Verified against PostgreSQL 16.14 in an isolated lab environment
Audit status
reviewed
ShareLinkedInX

Went further?

Pro unlocks the second lab proof

Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.

FollowSubstackLinkedInnew errors · lab notes · hiring loops