← All guides

JSON to Zod

Quick Answer

Use the JSON Schema Generator with the Zod tab selected. Paste your JSON to generate Zod schemas for runtime validation. Zod provides TypeScript-like type safety at runtime, perfect for validating API responses and user input.

What is Zod?

Zod is a TypeScript-first schema validation library with static type inference. Unlike TypeScript interfaces (which only exist at compile time), Zod schemas validate data at runtime, throwing errors if data doesn't match the expected structure.

Why Use Zod?

  • Runtime Validation: Catch bad data before it breaks your app
  • Type Safety: Infer TypeScript types from your schemas
  • Great DX: Excellent error messages for debugging
  • Composable: Build complex schemas from simple ones
  • Zero Dependencies: Lightweight and fast

Step-by-Step Instructions

  1. Open the tool: Navigate to the JSON Schema Generator.
  2. Select Zod tab: Click on the "Zod" tab to generate Zod schemas.
  3. Paste your JSON: Copy a representative JSON sample and paste it into the input area.
  4. Review the schema: The tool generates a Zod schema that matches your data structure.
  5. Copy the code: Click the copy button to copy the generated schema.
  6. Use in your project: Import Zod and use the schema to validate data.

Example

Input JSON:

{
  "id": 123,
  "email": "user@example.com",
  "age": 25,
  "isVerified": false
}

Generated Zod Schema:

import { z } from 'zod';

const UserSchema = z.object({
  id: z.number(),
  email: z.string(),
  age: z.number(),
  isVerified: z.boolean()
});

type User = z.infer<typeof UserSchema>;

Using the Schema:

// Validate unknown data
const result = UserSchema.safeParse(apiResponse);

if (result.success) {
  // result.data is typed as User
  console.log(result.data.email);
} else {
  // result.error contains validation errors
  console.error(result.error.issues);
}

Advanced Features

String Validation

The tool generates basic string schemas. You can enhance them:

const UserSchema = z.object({
  email: z.string().email(),  // Validates email format
  name: z.string().min(2).max(50),  // Length constraints
  phone: z.string().regex(/^d{10}$/)  // Custom regex
});

Number Validation

const ProductSchema = z.object({
  price: z.number().positive(),  // Must be > 0
  quantity: z.number().int().min(0),  // Integer >= 0
  rating: z.number().min(1).max(5)  // Range validation
});

Optional Fields

const UserSchema = z.object({
  id: z.number(),
  name: z.string(),
  bio: z.string().optional()  // Optional field
});

Best Practices

  • Validate at boundaries: Validate all external data (APIs, forms, files)
  • Use .safeParse(): For user input to handle errors gracefully
  • Use .parse(): For internal data where errors should throw
  • Compose schemas: Build reusable schemas for common patterns

Related Tools

FAQ

Q: Do I need both TypeScript and Zod?
A: They complement each other. TypeScript provides compile-time safety, Zod provides runtime safety. You can infer TypeScript types from Zod schemas.

Q: How do I handle optional fields?
A: The tool generates required fields by default. Add .optional() to make fields optional, or use .nullable() for null values.

Q: Can I customize validation rules?
A: Yes! The generated schema is a starting point. Add .email(), .min(), .max(), and other validators as needed.

Related tools