TypeScript

JSON Schema to Zod examples

Convert a JSON Schema document into Zod validators and inferred TypeScript types in your browser.

Object with required, enum, and nullable fields

A typical JSON Schema object with required keys, an enum, formats, and a nullable field.

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "User",
  "type": "object",
  "required": [
    "id",
    "email",
    "role"
  ],
  "properties": {
    "id": {
      "type": "string",
      "format": "uuid"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "role": {
      "type": "string",
      "enum": [
        "admin",
        "editor",
        "viewer"
      ]
    },
    "nickname": {
      "type": [
        "string",
        "null"
      ]
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  }
}
Open example

Shared subschemas with $defs and $ref

A root schema that references reusable subschemas defined under $defs.

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Order",
  "type": "object",
  "required": [
    "id",
    "items"
  ],
  "properties": {
    "id": {
      "type": "string"
    },
    "customer": {
      "$ref": "#/$defs/Customer"
    },
    "items": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/LineItem"
      }
    }
  },
  "$defs": {
    "Customer": {
      "type": "object",
      "required": [
        "name"
      ],
      "properties": {
        "name": {
          "type": "string"
        },
        "website": {
          "type": "string",
          "format": "uri"
        }
      }
    },
    "LineItem": {
      "type": "object",
      "required": [
        "sku",
        "quantity"
      ],
      "properties": {
        "sku": {
          "type": "string"
        },
        "quantity": {
          "type": "integer"
        }
      }
    }
  }
}
Open example

Draft 7 definitions

A Draft 7 document that keeps shared schemas under definitions instead of $defs.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "Address": {
      "type": "object",
      "required": [
        "city"
      ],
      "properties": {
        "street": {
          "type": "string"
        },
        "city": {
          "type": "string"
        },
        "zip": {
          "type": "string"
        }
      }
    }
  }
}
Open example

Use these examples with guides