Skip to content

toPostgresJson

toPostgresJson(json): string

Defined in: packages/pipeline/src/helpers/postgres-json.ts:36

Makes JSON text acceptable to a PostgreSQL jsonb (or json) parameter.

PostgreSQL rejects two things that JSON.stringify legitimately produces: a NUL character (\u0000, “unsupported Unicode escape sequence”) and a lone UTF-16 surrogate (\ud800, “invalid input syntax for type json”). Either one fails the whole INSERT, so a single stray character in a payload or error message would lose the entire row. This replaces each with U+FFFD, the Unicode replacement character, in keys and values alike, and leaves every other character and every valid surrogate pair unchanged.

The replacement is lossy by design: jsonb has no representation for these characters. Use it where keeping the record matters more than those characters, such as audit and dead-letter rows.

string

JSON text, such as the output of JSON.stringify.

string

The same JSON text with the unsupported escapes replaced.

toPostgresJson(JSON.stringify({ note: 'a\u0000b' })); // '{"note":"a\\ufffdb"}'