Metadata and document fields
Store structured, schemaless application data in a PostgreSQL table.
Drizzle ORM · Function
Drizzle's json() builder declares a PostgreSQL JSON column. $type<T>() adds compile-time inference and checking for JSON values across defaults, inserts, and selects.
The json() column builder maps a PostgreSQL JSON column into a Drizzle schema. Add $type<T>() to describe the TypeScript shape and receive compile-time checking for defaults, inserts, and selected values; this does not validate runtime data.
json(); json().$type<T>(); json().default(value); json().default(sql`...`);A PostgreSQL JSON column builder used in a pgTable schema.
json() represents textual JSON data according to RFC 7159.$type<T>() when you want a JSON column to infer a particular object, array, or primitive shape.$type<T>() only affects compile-time inference; it does not validate values at runtime.import { json, pgTable } from "drizzle-orm/pg-core";
export const table = pgTable("table", {
metadata: json().$type<{ foo: string }>().default({ foo: "bar" }),
});Defines a PostgreSQL table with a JSON column whose inferred TypeScript shape is checked for the default value.
Store structured, schemaless application data in a PostgreSQL table.
Make JSON objects or arrays statically typed in TypeScript.
Provide an object literal or SQL expression as the column default.
Use json() from drizzle-orm/pg-core as a field in a pgTable definition. It represents PostgreSQL's JSON data type in the Drizzle schema.
Follow-up: How can the JSON value's TypeScript shape be constrained? Add $type<T>() to the builder, such as json().$type<string[]>().
$type<T>() do on a JSON column?Call $type<T>() on the JSON builder, where T is the desired TypeScript shape. Drizzle then checks that shape in defaults, inserts, and selected results at compile time.
Follow-up: What limitation remains? $type<T>() cannot perform runtime validation of values.
A JSON column can receive a JavaScript object directly as a default, or a SQL expression through sql, such as sql\'{"foo": "bar"}'\. The generated schema uses PostgreSQL's json type.
Follow-up: Why use a SQL expression for a default? It lets the database receive an explicit SQL default expression rather than relying on a TypeScript object literal.
pgTablesql$type<T>()PostgreSQL json