DevBolt
Processed in your browser. Your data never leaves your device.

How do I convert JSON or JSON Schema to Zod?

Paste JSON data (the tool infers types) or a JSON Schema (precise conversion with $ref, allOf/oneOf/anyOf, format constraints, and defaults). The output is a production-ready Zod schema with .optional(), .strict(), coerce, and .describe() options. Download as .ts or copy. Everything runs in your browser.

Simple object to Zod schema
Input
{"name":"Alice","age":28,"email":"a@b.com"}
Output
const schema = z.object({
  name: z.string(),
  age: z.number(),
  email: z.string().email(),
});
← Back to tools

JSON to Zod Converter

Convert JSON or JSON Schema to Zod validation schemas. Supports $ref, allOf/oneOf/anyOf, enum, format constraints, and nested objects.

Input:

About JSON to Zod Converter

  • Two input modes — paste raw JSON data to infer schemas, or paste a JSON Schema for precise conversion with $ref, allOf/ oneOf/ anyOf, and constraints.
  • JSON Schema support — converts required, enum, const, format (email, uri, uuid, date-time, ipv4, ipv6), pattern, minimum/maximum, minLength/maxLength, default, and $defs/definitions.
  • Smart inference — detects emails, URLs, UUIDs, and ISO dates in raw JSON values and adds appropriate Zod refinements.
  • Required vs optional — JSON Schema required arrays map to required fields; all others get .optional().
  • TypeScript type — generates z.infer<typeof schema> so you get TypeScript types for free.
  • Everything runs in your browser — no data is sent over the network.

Tips & Best Practices

Pro Tip

Add .transform() for runtime data coercion

Generated Zod schemas validate shape, but API data often needs transformation — date strings to Date objects, string numbers to actual numbers, empty strings to null. Chain .transform() after .parse() to clean data in the same step as validation.

Common Pitfall

z.infer<typeof schema> doesn't capture .transform() output types

z.infer gives you the input type. If your schema uses .transform(), you need z.output<typeof schema> for the transformed type. Using z.infer with transforms means your TypeScript types won't match the actual runtime values.

Real-World Example

Validate API responses at the boundary

Parse every external API response through a Zod schema: const data = UserSchema.parse(await res.json()). This catches type mismatches immediately at the boundary rather than letting bad data propagate through your app and cause cryptic errors downstream.

Security Note

Use .strict() to reject unexpected fields

By default, Zod strips unknown keys. If an attacker sends extra fields (isAdmin: true, role: 'superuser'), they're silently removed. But if you spread the parsed object into a database query, those fields might sneak through. Use .strict() to throw on unexpected fields instead of silently dropping them.

Frequently Asked Questions

How do I convert JSON to a Zod validation schema?
Paste JSON data or a JSON Schema document into DevBolt's converter. For JSON data, the tool infers Zod types from values: strings become z.string(), numbers become z.number(), booleans become z.boolean(), arrays become z.array(), and nested objects become z.object(). Common string formats like emails, URLs, and UUIDs are detected and add appropriate Zod refinements. For JSON Schema input, constraints like minLength, pattern, minimum, and enum map directly to Zod validation methods for precise conversion.
What is the difference between converting JSON data vs JSON Schema to Zod?
Converting from JSON data infers types from actual values, which is quick but may miss constraints. A string 'hello' becomes z.string() but you would not know it should be an email. Converting from JSON Schema provides precise types with all constraints: format, pattern, minLength, maxLength, minimum, maximum, required fields, and enum values all map to specific Zod methods. Use JSON data conversion for rapid prototyping and JSON Schema conversion for production-grade validation schemas.
How does the converter handle nullable and optional fields?
Null values in JSON data produce z.nullable() types. Fields present in some array objects but missing in others become .optional(). In JSON Schema, nullable: true maps to .nullable() and fields not listed in the required array become .optional(). You can enable both behaviors simultaneously for fields that can be null, undefined, or present. Toggle options control whether to add .optional(), .strict(), coerce mode, .describe(), and .default() to the generated schema.

Related Convert Tools