method

SavepointSQL.json

value: unknown
): undefined | string | SQLJSONParameter;

Returns a parameter that stores value as JSON, to interpolate into a query. It works on every adapter.

Bun sends an object or an array as JSON by itself, and PostgreSQL stores a string in a json or jsonb column as a JSON string. Use sql.json to store a JSON number, boolean or null, to store an object or an array on SQLite, and to store a JSON string where Bun sends a string as it is: MySQL, MariaDB, SQLite, and PostgreSQL with prepare: false.

await sql`INSERT INTO settings (data) VALUES (${sql.json(42)})`; // 42
await sql`INSERT INTO settings (data) VALUES (${sql.json(null)})`; // JSON null, not SQL NULL
await sql`INSERT INTO settings (data) VALUES (${sql.json("dark")})`; // "dark"

Referenced types

interface SQLJSONParameter

A JSON parameter created by SQL.json on PostgreSQL