Validate 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
Validate
4 functionsConformance checks. json_schema_is_valid returns TRUE/FALSE and json_schema_errors lists each failure (both new in the DuckDB 2.0 build). json_schema_validate and json_schema_validate_schema return TRUE on success and raise on failure; on the Haybarn 1.5 build wrap them with COALESCE(TRY(...), false) when a boolean filter is required.
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
schema
|
Type
JSON
|
Mode Positional |
Description
The JSON Schema document. The DuckDB 2.0 build supports drafts 4, 6, 7, 2019-09 and 2020-12, selected by $schema (2020-12 when absent); an unknown $schema is an error. An invalid schema raises an error.
|
Argument
json_data
|
Type
JSON
|
Mode Positional |
Description
The JSON value to check. Pass a column of type JSON, a struct literal, or a string cast with ::JSON.
|
Returns
LIST(STRUCT(path VARCHAR, keyword VARCHAR, message VARCHAR)) — one entry per validation error, empty for a conforming document. path is a JSON Pointer into the document (empty string for the root). SQL NULL inputs propagate NULL.
Description
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. Use it to build data-quality reports or quarantine tables that record why each row was rejected. An invalid schema raises an error. New in the DuckDB 2.0 build.
SELECT unnest(json_schema_errors('{"type": "array", "items": {"type": "integer"}}', '[1, "two", 3, true]'), recursive := true);
SELECT json_schema_errors('{"type": "object", "required": ["id", "name"]}', '{"id": 1}') AS errors;
SELECT row_num, e.path, e.keyword, e.message
FROM events,
unnest(json_schema_errors('{"type":"object","properties":{"id":{"type":"integer"}},"required":["id"]}', payload)) AS t(e)
ORDER BY row_num;
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
schema
|
Type
JSON
|
Mode Positional |
Description
The JSON Schema document. The DuckDB 2.0 build supports drafts 4, 6, 7, 2019-09 and 2020-12, selected by $schema (2020-12 when absent); an unknown $schema is an error. An invalid schema raises an error.
|
Argument
json_data
|
Type
JSON
|
Mode Positional |
Description
The JSON value to check. Pass a column of type JSON, a struct literal, or a string cast with ::JSON.
|
Returns
BOOLEAN — TRUE for conforming data, FALSE otherwise; raises an error only for an invalid schema. SQL NULL inputs propagate NULL.
Description
Return whether a JSON value conforms to a JSON Schema. Unlike json_schema_validate a nonconforming document returns FALSE instead of raising, so it can be used directly in WHERE and FILTER clauses without TRY; an invalid schema still raises an error, so a broken contract is never mistaken for bad data. New in the DuckDB 2.0 build.
SELECT json_schema_is_valid('{"type": "integer", "minimum": 0}', '-1') AS ok;
-- false
SELECT json_schema_is_valid('{"prefixItems": [{"type": "string"}, {"type": "integer"}]}', '[1, "a"]') AS ok;
-- false
SELECT
COUNT(*) FILTER (WHERE json_schema_is_valid('{"type":"object","properties":{"id":{"type":"integer"}},"required":["id"]}', payload)) AS valid,
COUNT(*) FILTER (WHERE NOT json_schema_is_valid('{"type":"object","properties":{"id":{"type":"integer"}},"required":["id"]}', payload)) AS invalid
FROM events;
Related functions
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
schema
|
Type
JSON
|
Mode Positional |
Description
The JSON Schema document to validate against. The DuckDB 2.0 build supports drafts 4, 6, 7, 2019-09 and 2020-12, selected by $schema (2020-12 when absent); an unknown $schema is an error. The Haybarn 1.5 build targets draft-07 only.
|
Argument
json_data
|
Type
JSON
|
Mode Positional |
Description
The JSON value to validate. Pass a column of type JSON, a struct literal, or a string cast with ::JSON.
|
Returns
BOOLEAN — TRUE for conforming data; raises an error for nonconforming data or an invalid schema. SQL NULL inputs propagate NULL.
Description
Validate a JSON value against a JSON Schema. Validation failures raise an error; they do not return FALSE. In the DuckDB 2.0 build the error names up to five failing locations (as JSON Pointers) without echoing the document; use json_schema_is_valid for a boolean result and json_schema_errors for a structured report. On the Haybarn 1.5 build, use COALESCE(TRY(json_schema_validate(schema, payload)), false) for row filtering, after validating the schema separately (TRY also catches malformed-schema errors).
SELECT json_schema_validate('{
"$schema": "https://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"}
},
"required": ["id"]
}', {'id': 5, 'name': 'George'}) AS ok;
-- TRUE
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"]}';
INSERT INTO events_clean
SELECT * FROM events_raw
WHERE COALESCE(TRY(json_schema_validate(getvariable('event_schema'), payload)), false);
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
COUNT(*) AS total,
COUNT(*) FILTER (WHERE COALESCE(TRY(json_schema_validate(getvariable('event_schema'), payload)), false)) AS valid,
COUNT(*) FILTER (WHERE NOT COALESCE(TRY(json_schema_validate(getvariable('event_schema'), payload)), false)) AS invalid
FROM events;
Related functions
- json_schema_is_valid() — Return whether a JSON value conforms to 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`
- json_schema_validate_schema() — Check that a JSON Schema compiles
- 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_update() — Apply the schema's `default` values inline
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
schema
|
Type
JSON
|
Mode Positional |
Description
Candidate JSON Schema. The function compiles it, which catches malformed JSON, unsupported $schema drafts, invalid regular expressions and unresolvable $refs; it is not exhaustive validation against the JSON Schema meta-schema.
|
Returns
BOOLEAN — TRUE if the schema is accepted; raises an error if it is invalid. SQL NULL propagates NULL.
Description
Check that a JSON Schema compiles. An invalid schema raises an error, making this useful as a deployment gate. Use COALESCE(TRY(json_schema_validate_schema(definition)), false) when auditing a registry and you want invalid entries flagged instead of aborting the query. Compiling is not exhaustive meta-schema validation; some misspelled keywords are simply ignored, as the JSON Schema specification requires.
SELECT json_schema_validate_schema('{
"$schema": "https://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"id": {"type": "integer"}
}
}') AS schema_ok;
-- TRUE
SELECT name, version
FROM schema_registry
WHERE NOT COALESCE(TRY(json_schema_validate_schema(definition)), false);