Skip to content

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 functions

json_schema_patch

Scalar function Enrich
Signature
json_schema_patch(schema: JSON, json_data: JSON) → JSON
Arguments (Positional)
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
Description
1 Generate a patch for a partial document
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}]
2 Audit patches; NULL marks input or schema errors
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;

json_schema_update

Scalar function Enrich
Signature
json_schema_update(schema: JSON, json_data: JSON) → JSON
Arguments (Positional)
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
Description
1 Fill in a missing default
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"}
2 Build a clean table with defaults applied
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);