Skip to content

DuckDB Extension

JSON Schema

Validate JSON documents against a JSON Schema in DuckDB SQL. Check required fields and value types, or fill in defaults defined by your schema.

1,061,706
extension loads · last 90 days
On this page
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);

Check that JSON documents follow an agreed structure

A JSON Schema describes a document’s expected fields, types, and constraints. DuckDB can read JSON with different shapes; this extension adds an explicit check against the contract you choose.

Use it to check API payloads or incoming records before further processing. The DuckDB 2.0 build supports JSON Schema drafts 4 through 2020-12 and adds boolean and structured-error functions; the Haybarn 1.5 build targets draft-07. Both can apply schema-defined defaults.

Example

Check two JSON documents

The schema requires an integer id. The first document passes; the second has a string where an integer is required. All sample data is included.

Tested with DuckDB 1.5.6. Browser examples use Haybarn-Wasm 2.0.0-rc12.

SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
  WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL json_schema FROM query_farm;
LOAD json_schema FROM query_farm;

WITH schema AS (
  SELECT '{
    "type": "object",
    "properties": {"id": {"type": "integer"}},
    "required": ["id"]
  }'::JSON AS definition
), events(row_num, payload) AS (
  VALUES
    (1, '{"id": 42}'::JSON),
    (2, '{"id": "oops"}'::JSON)
)
SELECT row_num, payload,
  COALESCE(
    TRY(json_schema_validate(definition, payload)), false
  ) AS valid
FROM events CROSS JOIN schema
ORDER BY row_num;

Expected result

row_num payload valid
1 {"id":42} true
2 {"id":"oops"} false

Validation raises an error for a nonconforming document. TRY turns that error into NULL; COALESCE makes the result false. Validate a schema separately before using this pattern with your own data, because TRY also catches errors in the schema. In the DuckDB 2.0 build, json_schema_is_valid(definition, payload) returns the boolean directly.

Common tasks

Deep Dive

Technical Details

Reference

Extension Contents

Quick reference to all available functions and settings organized by category.

Enrich
json_schema_patch() Object type: Scalar function 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() Object type: Scalar function Apply the schema's default values inline.
Validate
json_schema_errors() Object type: Scalar function 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_is_valid() Object type: Scalar function Return whether a JSON value conforms to a JSON Schema.
json_schema_validate() Object type: Scalar function Validate a JSON value against a JSON Schema.
json_schema_validate_schema() Object type: Scalar function Check that a JSON Schema compiles.

API Reference

Function Reference

Practical Examples

Cookbook

Real-world recipes and patterns for common use cases.

Platform Support

Compatibility

Extension availability may vary by platform and DuckDB version. Check below to ensure this extension supports your environment before installation.

Quick Facts

Release status Stable
Software License MIT
Pricing Free
Written In C++
Source Available Yes
View on GitHub
Usage
1,061,706
loads · last 90 days

Platforms

DuckDB 2.0.0

  • Linux x86_64aarch64
  • Linux (musl) Not available
  • macOS Apple Silicon
  • Windows x86_64x86_64 (MinGW)
  • WASM Not available
Compiled binary sizes
Platform Architecture Size
Linux x86_64 4.18 MB
Linux aarch64 3.53 MB
macOS Apple Silicon 2.97 MB
Windows x86_64 10.34 MB
Windows x86_64 (MinGW) 18.01 MB

Compressed download sizes for the release shown above.

DuckDB & Haybarn

Release calendar
  • DuckDB v2.0.0 Query.Farm repository Supported
  • Haybarn v1.5.6 Built-in community repository Supported

Browser examples use Haybarn-Wasm 2.0.0-rc12.

Checked .

Learn and explore

Tutorials and articles

Writing about JSON Schema: how it works, what changed, and why.