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

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 validator targets JSON Schema draft-07 and can apply schema-defined defaults. Validation errors normally stop the query; the example shows how to turn invalid documents into a boolean result after checking the schema itself.

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 1.5.6.

INSTALL json_schema FROM community;
LOAD json_schema;

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. The validator targets JSON Schema draft-07.

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_validate() Object type: Scalar function Validate a JSON value against a JSON Schema.
json_schema_validate_schema() Object type: Scalar function Check that the validator can load a JSON Schema.

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_64 aarch64
  • Linux (musl) Not available
  • macOS Apple Silicon
  • Windows x86_64 x86_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 1.5.6.

Checked .