Enrich functions in the JSON Schema DuckDB extension
Sample data for the examples
Each browser example starts with these sample tables; sample files are shared across examples on this page. Run this setup first when trying the examples in your own DuckDB session.
-- Concrete source data for validation, defaults, and quarantine examples.
-- Author-blessed schema-registry recipe (no bind variable).
CREATE OR REPLACE TABLE schema_registry AS
SELECT * FROM (VALUES
(
'events.v3',
1,
'{"$schema": "https://json-schema.org/draft-07/schema", "type": "object", "properties": {"id": {"type": "integer"}}}'::JSON
),
(
'events.v2',
1,
'{"$schema": "https://json-schema.org/draft-07/schema", "type": "object", "properties": {"id": {"type": "string"}}}'::JSON
)
) t(name, version, definition);
-- Source / sink / quarantine tables for the validation-and-routing recipes.
-- Payloads are intentionally varied — some valid against a typical "object
-- with id" schema, some not — so the documented TRY wrapper produces both valid and invalid rows.
CREATE OR REPLACE TABLE events AS
SELECT * FROM (VALUES
(1, '{"id": 1, "type": "click", "ts": "2026-04-28T12:00:00Z"}'::JSON),
(2, '{"id": 2, "type": "view"}'::JSON),
(3, '{"id": "not-a-number", "type": "click"}'::JSON),
(4, '{"type": "missing-id"}'::JSON),
(5, '{}'::JSON)
) t(row_num, payload);
CREATE OR REPLACE TABLE events_raw AS
SELECT * FROM events;
CREATE OR REPLACE TABLE events_clean (
row_num INTEGER,
payload JSON
);
CREATE OR REPLACE TABLE events_quarantine (
row_num INTEGER,
payload JSON
);
-- Local JSON input for the file-ingestion example.
COPY (SELECT * FROM (VALUES (1, 'Alice'), (2, 'Bob')) AS people(id, name))
TO 'events.json' (FORMAT JSON, ARRAY true);
Function category
Enrich
2 functionsDefault-value handling. json_schema_patch computes the JSON Patch (RFC 6902) needed to fill missing defaults; json_schema_update applies it inline.
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
schema
|
Type
JSON
|
Mode Positional |
Description
JSON Schema document. Properties whose default keyword is set are the ones that contribute to the patch.
|
Argument
json_data
|
Type
JSON
|
Mode Positional |
Description
The (possibly incomplete) JSON value. Missing properties for which the schema declares a default will appear in the resulting patch.
|
Returns
JSON — a JSON Patch (RFC 6902) array of add operations that, when applied, fill in the missing defaults.
Description
Compute the JSON Patch (RFC 6902) diff that would bring json_data into conformance with the default values declared by the schema. Returns the patch — does not apply it (use json_schema_update for the in-place variant).
Use the patch when you want to log or audit which fields were synthesized, or when you need to apply the same diff to multiple downstream representations of the document. The function also validates the document and raises on invalid values; defaults do not repair type mismatches. Use TRY for an audit column that should return NULL on errors, and validate the schema separately first.
SELECT json_schema_patch('{
"$schema": "https://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"id": {"type": "integer", "default": 5},
"name": {"type": "string"}
}
}', {'name': 'George'}) AS patch;
-- [{"op":"add","path":"/id","value":5}]
SET VARIABLE event_schema = '{"$schema":"https://json-schema.org/draft-07/schema","type":"object","properties":{"id":{"type":"integer","default":5},"name":{"type":"string"}},"required":["id"]}';
SELECT
payload,
TRY(json_schema_patch(getvariable('event_schema'), payload)) AS defaults_patch
FROM events_raw;
Related functions
- json_schema_update() — Apply the schema's `default` values inline
- json_schema_validate() — Validate a JSON value against a JSON Schema
- json_schema_errors() — Return every validation error of a JSON value against a JSON Schema as a structured list: where in the document it failed (`path`), which schema keyword failed (`keyword`) and a readable `message`
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
schema
|
Type
JSON
|
Mode Positional |
Description
JSON Schema document. Properties with default declared are filled in where the input is missing them.
|
Argument
json_data
|
Type
JSON
|
Mode Positional |
Description
The JSON value to enrich. Existing fields are preserved; only missing properties with declared default values are added.
|
Returns
JSON — the input value with missing defaults filled in.
Description
Apply the schema's default values inline. Equivalent to running json_schema_patch and applying the resulting patch — but in a single function call you can use directly in a SELECT projection.
This is the function to reach for when you want enriched JSON, not a diff. The function also validates the document and raises on invalid values; defaults do not repair type mismatches. Use TRY for an audit column that should return NULL on errors, and validate the schema separately first.
SELECT json_schema_update('{
"$schema": "https://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"id": {"type": "integer", "default": 5},
"name": {"type": "string"}
}
}', {'name': 'George'}) AS updated;
-- {"id":5,"name":"George"}
SET VARIABLE event_schema = '{"$schema":"https://json-schema.org/draft-07/schema","type":"object","properties":{"id":{"type":"integer","default":5},"name":{"type":"string"}},"required":["id"]}';
CREATE TABLE events_enriched AS
SELECT json_schema_update(getvariable('event_schema'), payload) AS payload
FROM events_raw
WHERE COALESCE(TRY(json_schema_validate(getvariable('event_schema'), payload)), false);
Related functions
- json_schema_patch() — Compute the JSON Patch (RFC 6902) diff that would bring `json_data` into conformance with the `default` values declared by the schema
- json_schema_validate() — Validate a JSON value against a JSON Schema
- json_schema_errors() — Return every validation error of a JSON value against a JSON Schema as a structured list: where in the document it failed (`path`), which schema keyword failed (`keyword`) and a readable `message`