SQL to JSON Schema
Paste CREATE TABLE SQL and get back a JSON Schema Draft 7 object with properties (column → typed entry with optional format) and a required array for NOT NULL columns without a default. Multiple tables produce a $defs map so you can $ref individual tables from an OpenAPI spec or validator.
How to use the SQL to JSON Schema
Paste one or more CREATE TABLE statements and click Generate JSON Schema. If a single table is pasted, the output is a standalone JSON Schema object. If multiple tables are pasted, the output is a JSON Schema with a $defs map containing one sub-schema per table.
- Type mapping — SQL integer types map to JSON Schema
"type": "integer"; VARCHAR and TEXT become"type": "string"; DECIMAL/FLOAT become"type": "number"; BOOLEAN becomes"type": "boolean"; DATE becomesstringwith"format": "date"; TIMESTAMP/DATETIME becomesstringwith"format": "date-time"; JSON/JSONB columns become"type": "object". - Required array — a column is added to
requiredif it hasNOT NULLand noDEFAULTvalue. Primary key columns (which are implicitly NOT NULL) are also required unless they are auto-increment (auto-generated server-side). - Copy and use — paste into an OpenAPI spec under
components/schemas, use with AJV, Zod, or any JSON Schema validator, or import into Postman for request validation.
What is JSON Schema and how does it relate to SQL tables?
JSON Schema is a vocabulary for annotating and validating JSON documents. A schema describes the expected shape of a JSON object — which properties exist, their types, which are required, and what format constraints apply. JSON Schema Draft 7 (and its successor Draft 2019-09 and 2020-12) is the standard used by OpenAPI 3.0, AJV, Zod, and most API validation frameworks. Generating schemas from SQL DDL bridges the gap between the database schema (which defines the canonical data shape) and the API layer (which exposes and validates it).
The type mapping between SQL and JSON Schema is straightforward for most types but has a few conventions worth noting. JSON Schema has no native date or timestamp type — instead, strings with a format keyword ("date", "date-time", "time") represent temporal values. JSON Schema distinguishes integer from number (which allows decimals) — SQL INT maps to integer, DECIMAL/FLOAT to number. SQL BOOLEAN maps directly to JSON Schema boolean. JSON/JSONB columns are emitted as object since their internal structure is not captured in the DDL.
The required array is derived from SQL nullability: a column is required in the JSON Schema sense when it must always be present and non-null. This corresponds to NOT NULL without a server-side default — columns with a DEFAULT can be omitted from API requests and the database fills in the value. Auto-increment primary keys are treated as optional since the client does not supply them on insert.
Common use cases
- OpenAPI request body schemas — paste the generated schema under
components/schemasin your OpenAPI spec and reference it in POST/PUT request body definitions. - API validation — use the schema with AJV or a Zod conversion to validate incoming JSON request bodies against the database schema at the API layer.
- Type generation — feed the JSON Schema into json-schema-to-typescript (json2ts) or similar tools to generate TypeScript interfaces that match the database tables.
- Postman / Insomnia validation — import the schema into Postman test scripts to auto-validate response payloads against the expected table shape.
- Documentation — JSON Schema has rich tooling for rendering human-readable docs (Redoc, Swagger UI); use the generated schemas to document API data models.