Skip to content

A JSON string sent to a json/jsonb column is stored as a JSON string, not the value it encodes #43

Open

Description

@okomarov

The json serializer in src/types.js is x => JSON.stringify(x), applied whenever the server reports a parameter as json or jsonb. A string is therefore encoded again:

await sql`insert into t (doc) values (${'{"a":1}'})`
// stored: "{\"a\":1}"  (a JSON string), not {"a": 1}

ORMs hit this constantly because they serialize JSON columns themselves before handing values to the driver. porsager#1139 (open) reports it with Drizzle's postgres-js driver, where db.insert(t).values({ data: { foo: 'bar' } }) stores a string. The workaround is a custom json serializer that passes strings through.

Storing a JSON string is sometimes what a caller wants, so neither behaviour is right for everyone. Options:

  • Pass strings through unchanged, so an already-serialized value is stored as the value it encodes. Callers who want a JSON string pass JSON.stringify(s) or use sql.json(s).
  • Keep the default and document the custom serializer for ORM users.

The first changes what is stored, so it belongs in a major version or behind the parameter-encoding change discussed in #41. It matters for adoption: Drizzle is the most common way Postgres.js is used.

Activity

  1. added
    bug
    on Oct 13, 2026
  2. moved this to In progress in Roadmapon Oct 15, 2026
  3. okomarov commented on Oct 15, 2026

    @okomarov
    Author

    Available in 4.5.0-rc1 with parameter_types: 'client' (npm install @steadybrew/postgresjs@next). parameter_types: 'warn' reports which of your queries 'client' would send differently; see Parameter types. This stays open until 'client' becomes the default in 5.0.

  4. added
    area: serialization
    upstream
    on Oct 16, 2026
  5. moved this from In progress to Later in Roadmapon Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: serializationbugupstream

    Type

    No type

    Projects

    • Status
      Later

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions