Skip to content

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 functions

json_schema_errors

Scalar function Validate
Signature
json_schema_errors(schema: JSON, json_data: JSON) → STRUCT(path VARCHAR, keyword VARCHAR, message VARCHAR)[]
Arguments (Positional)
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
Description
1 List the errors of one document
SELECT unnest(json_schema_errors('{"type": "array", "items": {"type": "integer"}}', '[1, "two", 3, true]'), recursive := true);
2 Missing required properties
SELECT json_schema_errors('{"type": "object", "required": ["id", "name"]}', '{"id": 1}') AS errors;
3 Report why rows fail
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;

json_schema_is_valid

Scalar function Validate
Signature
json_schema_is_valid(schema: JSON, json_data: JSON) → BOOLEAN
Arguments (Positional)
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
Description
1 Check a value
SELECT json_schema_is_valid('{"type": "integer", "minimum": 0}', '-1') AS ok;
-- false
2 2020-12 keywords such as prefixItems are enforced
SELECT json_schema_is_valid('{"prefixItems": [{"type": "string"}, {"type": "integer"}]}', '[1, "a"]') AS ok;
-- false
3 Count valid and invalid rows
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;

json_schema_validate

Scalar function Validate
Signature
json_schema_validate(schema: JSON, json_data: JSON) → BOOLEAN
Arguments (Positional)
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
Description
1 Validate a single document
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
2 Filter a column on schema conformance
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);
3 Aggregate valid / invalid counts
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;

json_schema_validate_schema

Scalar function Validate
Signature
json_schema_validate_schema(schema: JSON) → BOOLEAN
Arguments (Positional)
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
Description
1 Lint a schema before deploying
SELECT json_schema_validate_schema('{
  "$schema": "https://json-schema.org/draft-07/schema",
  "type": "object",
  "properties": {
    "id": {"type": "integer"}
  }
}') AS schema_ok;
-- TRUE
2 Find any malformed schemas in a registry table
SELECT name, version
FROM schema_registry
WHERE NOT COALESCE(TRY(json_schema_validate_schema(definition)), false);