Skip to content

[Pg]: Fix json/jsonb double-parsing with node-postgres driver#5524

Closed
Sim-hu wants to merge 1 commit intodrizzle-team:mainfrom
Sim-hu:fix/node-pg-jsonb-double-parse
Closed

[Pg]: Fix json/jsonb double-parsing with node-postgres driver#5524
Sim-hu wants to merge 1 commit intodrizzle-team:mainfrom
Sim-hu:fix/node-pg-jsonb-double-parse

Conversation

@Sim-hu
Copy link
Copy Markdown

@Sim-hu Sim-hu commented Mar 22, 2026

Fixes #5485

Problem

node-postgres (pg) already parses json/jsonb columns via its built-in type parser (OIDs 114 and 3802), returning JS objects/values. Drizzle's mapFromDriverValue then calls JSON.parse again on the result.

For most values this is harmless — objects and arrays pass the typeof value === 'string' guard without being re-parsed. But JSON string scalars get double-parsed:

stored: "\"0.1\""  →  pg returns: "0.1"  →  drizzle parses: 0.1 (number)

This silently converts string values to numbers/booleans, which can cause data corruption.

Fix

Override the JSON and JSONB type parsers in the node-postgres session to return raw strings, matching the existing pattern already used for timestamps, dates, and intervals. This lets mapFromDriverValue handle the single parse correctly.

Notes

  • Same pattern as the existing TIMESTAMPTZ/TIMESTAMP/DATE/INTERVAL overrides in the same file
  • Only affects node-postgres — other drivers that return raw strings are unaffected
  • mapToDriverValue (JSON.stringify on write) remains unchanged

node-postgres (pg) already parses json/jsonb columns via its built-in
type parser, so drizzle's mapFromDriverValue would double-parse scalar
string values like "0.1", silently converting them to numbers.

Override the JSON (114) and JSONB (3802) type parsers to return raw
strings, matching the existing pattern used for timestamps and dates.

Fixes #5485
@Sim-hu
Copy link
Copy Markdown
Author

Sim-hu commented Mar 22, 2026

Closing this — I missed that #5487 already addresses the same issue. Also noted AlexBlokh's comment about the upcoming codecs system handling this properly across all drivers.

@Sim-hu Sim-hu closed this Mar 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: node-postgres driver implicitly converts numeric looking strings to numbers

1 participant