Skip to content

JSON Schema

JSON Schema validation and default-value enrichment in SQL.

32,894
extension loads Β· last 90 days
On this page

Technical Overview

JSON contracts, made executable in SQL

JSON Schema as an in-SQL contract

  • β€’ Draft-07 is the target: The underlying validator primarily targets draft-07. The $schema keyword in your document is honored where the validator supports it; for newer drafts (2019-09, 2020-12), test the specific keywords you use rather than assuming full coverage.
  • β€’ What the validator covers: Coverage matches what draft-07 specifies: type checks (string, number, integer, boolean, array, object, null), required, properties, numeric minimum / maximum / multipleOf, string minLength / maxLength / pattern / format, array minItems / maxItems / uniqueItems, and the default keyword that powers default-value enrichment.
  • β€’ Schema and data are both JSON: Both the schema and the value under test are passed as DuckDB JSON values β€” a column, a struct literal, or a string cast with ::JSON. There's no preregistration step, no ATTACH, no secret type, and no network: validation runs locally against the in-query schema string.
  • β€’ Default-value enrichment, not just validation: The non-obvious capability: turn the schema's declared default values into actual data. One function computes the JSON Patch (RFC 6902) diff of add operations (useful for an audit log); another applies them inline β€” taking an incomplete payload to a fully populated record in one expression, with no COALESCE cascade.

What to know before relying on it

  • β€’ Boolean result, not an error report: Validation returns TRUE / FALSE, not a structured list of which keywords or paths failed. For per-keyword diagnostics, validate at the application layer with ajv, Pydantic, or jsonschema, and use this extension as the coarse SQL gate at ingest, in CI, or in an audit.
  • β€’ No cross-file $ref resolution: Each call takes a single JSON schema value β€” there is no filesystem or HTTP $ref resolver. Inline any $ref targets into the same schema document before validating.
  • β€’ Prefer typed columns where you can: If the data has a fixed shape, modeling it as proper DuckDB columns plus CHECK constraints is cheaper, clearer, and gives better error messages. JSON Schema validation is for the genuinely-JSON case where the shape is contract-defined but the storage stays opaque.
  • β€’ Per-row cost scales with schema complexity: Deeply-nested schemas with many keywords run the full validator on every row. For very large tables, validate once on ingest into a staging table rather than re-running the check on every read.

Deep Dive

Technical Details

Install

INSTALL json_schema FROM community;
LOAD json_schema;

Quick Start

Validate a JSON document against a schema

SELECT json_schema_validate(:schema, payload) AS ok
FROM events;

Verify the schema itself is well-formed

SELECT json_schema_validate_schema(:schema) AS schema_ok;

Compute a JSON-Patch that fills in missing defaults

SELECT json_schema_patch(:schema, payload) AS patch FROM events;

Or apply the defaults inline

SELECT json_schema_update(:schema, payload) AS enriched FROM events;

Reference

Extension Contents

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

Name Description
Enrich
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.
Validate
json_schema_validate() Validate a JSON value against a JSON Schema.
json_schema_validate_schema() Lint a JSON Schema before applying it.

API Reference

Function Documentation

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
32,894
loads Β· last 90 days

Platforms

  • Linux x86_64 aarch64
  • Linux (musl) Not available
  • macOS Intel Apple Silicon
  • Windows x86_64
  • WASM eh mvp threads
Compiled binary sizes
Platform Architecture Size
Linux x86_64 3.42 MB
Linux aarch64 3.04 MB
macOS Intel 1.61 MB
macOS Apple Silicon 1.47 MB
Windows x86_64 7.51 MB
WASM eh 149.8 KB
WASM mvp 162.6 KB
WASM threads 149.9 KB

Compressed download size from the Haybarn extension repository.

DuckDB & Haybarn

Release calendar