JSON Schema Draft 2020-12 to Zod v4, Pydantic v2 & TypeScript
Compile Draft 2020-12 and Draft-07 JSON Schemas with $defs, $ref, enums, and bounds into Zod v4, Pydantic v2, and TypeScript interfaces locally via AJV & IR.
To compile JSON Schema Draft 2020-12 to Zod, Pydantic, and TypeScript without contract drift, validate payloads locally with AJV (Draft 2020-12 vs Draft-07), resolve #/$defs/ and #/definitions/ $ref pointers through DataTo's Unified Type IR, and emit Zod v4 validators, topologically ordered Pydantic v2 BaseModels, and TypeScript interfaces in browser RAM.
AJV Draft 2020-12 vs. Draft-07 and JSON Schema $ref codegen
Modern API contracts commonly target Draft 2020-12 ($defs and prefixItems tuples), while legacy OpenAPI 3.0 and backend specs still use Draft-07 (definitions and array items). Checking payloads upfront prevents subtle validation drift between TypeScript clients and Python services.
When you compile JSON Schema to code in DataTo, JSON Schema Validator automatically inspects $schema to select Ajv 2020-12 or Ajv Draft-07, checks email, uuid, uri, date, date-time, and ipv4 formats, and reports itemized JSON Pointer paths. Next, the Unified Type IR parser resolves #/$defs/ and #/definitions/ $ref pointers, allOf merges, anyOf/oneOf unions, and nullable flags into a single strongly typed AST.
Exact mapping across Zod v4, Pydantic v2, and TypeScript
Because every target generator reads the same Unified Type IR, enums, numeric bounds, string length limits, regex patterns, and field descriptions translate consistently across TypeScript and Python.
| JSON Schema Construct | Zod v4 (.ts) | Pydantic v2 (.py) | TypeScript (.d.ts) |
|---|---|---|---|
| type: string, format: email | z.email() | str | string |
| type: integer, minimum: 1, maximum: 100 | z.int().min(1).max(100) | int = Field(ge=1, le=100) | number |
| enum: ["open", "closed"] | z.enum(["open", "closed"]) | Literal["open", "closed"] | "open" | "closed" |
| additionalProperties: false | .strict() | Typed BaseModel fields | interface / type fields |
| description: "..." | .describe("...") | Field(description="...") | /** ... */ JSDoc comment |
Resolving $defs, $ref, constraints, and aliases cleanly
In Pydantic v2 output, nested classes extracted from $defs or nested properties are ordered topologically before parent classes so Python never raises forward-reference NameErrors. Field names containing hyphens, dots, or Python keywords automatically receive safe identifiers paired with Field(alias=...) and ConfigDict(populate_by_name=True).
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$defs": {
"Address": {
"type": "object",
"required": ["country"],
"properties": {
"country": { "type": "string", "minLength": 2, "maxLength": 2 }
}
}
},
"type": "object",
"required": ["email", "tier", "retries", "address"],
"properties": {
"email": { "type": "string", "format": "email" },
"tier": { "enum": ["free", "pro"] },
"retries": { "type": "integer", "minimum": 0, "maximum": 5 },
"address": { "$ref": "#/$defs/Address" }
}
}Emitting Zod v4, TypeScript .d.ts, or bootstrapping from sample JSON
JSON Schema to Zod emits Zod v4 primitives (z.email(), z.uuid(), z.url(), z.iso.datetime(), z.int()) alongside an optional z.infer type alias, while JSON Schema to TypeScript produces clean exported interfaces or type aliases with JSDoc descriptions. Need a starting schema first? Paste a sample payload into JSON to JSON Schema to infer a Draft 2020-12 or Draft-07 document locally.
import { z } from "zod";
export const AccountSchema = z.object({
email: z.email(),
tier: z.enum(["free", "pro"]),
retries: z.int().min(0).max(5),
address: z.object({
country: z.string().min(2).max(2),
}),
});
export type Account = z.infer<typeof AccountSchema>;Tools used in this guide
Schemas & MCP →Compile JSON Schema ($defs, $ref, enum, minimum, minLength, description) into idiomatic Zod v4 TypeScript code.
Transform JSON Schema specs (enum, minimum, description, $ref) into Python Pydantic v2 BaseModel classes with Field() bounds.
Compile JSON Schema into strict TypeScript .d.ts interfaces with union literals, optional ? props, and JSDoc comments.
Validate payloads against Draft 2020-12 or Draft-07 schemas locally with AJV and inspect exact JSON Pointer error paths.
JSON to JSON SchemaInfer Draft 2020-12 or Draft-07 JSON Schema with nested types, formats, required arrays, and strict additionalProperties locally.
JSON to TypeScriptExtract modular TypeScript interface or type definitions with optional readonly modifiers from nested JSON locally.
Related formats
Related Comparisons
Frequently asked questions
How does DataTo handle circular or recursive $ref pointers?
When the IR resolver detects a cyclic $ref, it emits a symbolic reference (rendered as z.lazy(() => ...) in Zod) and logs a non-fatal CYCLIC_REF diagnostic.
Does JSON Schema to Pydantic support Python Pydantic v1 as well as v2?
Yes. Switch Pydantic version target to Pydantic v1 (Legacy) to generate Optional[...], List[...], and inner Config classes instead of v2 unions and ConfigDict.
Are nested properties kept optional when omitted from required[]?
Yes. Properties absent from the object's required array map to .optional() in Zod, | None = None in Pydantic v2, and ?: in TypeScript.
Authoritative References
More engineering guides
Scrub DB URIs, Bearer JWTs, AWS keys, and IPs from logs, mask JSON keys validly, scale CSV salaries by ×k, and lock sessions in AES-256-GCM .redactkey.
JSON Schema vs. Zod v4 vs. Pydantic v2 for LLM Structured OutputsHow OpenAI Strict Mode, Anthropic Tool Use, Zod v4, and Pydantic v2 map through a Unified Type IR—and how to generate all three locally.
How to Repair Malformed & Truncated LLM JSON Outputs LocallyAutomatically strip markdown code fences, fix single quotes, trailing commas, Python True/False literals, and truncated brackets from LLM JSON responses.
OpenAPI to MCP: Exposing REST APIs as Model Context Protocol ToolsTurn internal OpenAPI 3.x YAML/JSON specs into Model Context Protocol (MCP) TypeScript tools and Zod v4 input schemas locally without uploading internal APIs.