JSON to Zod Schema Generator

JSON Input
1
Root Name
Zod Schema Output

Last updated:

Jsonic's JSON to Zod generator converts a JSON object or array into a complete Zod schema in one click. It generates z.object() for objects, z.array() for arrays, z.string(), z.number().int(), z.number(), z.boolean(), and z.null() for primitives. Fields that are null in the example get .nullable(). Mixed-type arrays produce z.union(). Nested objects become separate named schemas declared before the root. The output includes an import { z } from 'zod' statement and a TypeScript type export using z.infer<>. All conversion happens in your browser with no data upload.

How to convert JSON to a Zod schema

  1. Paste your JSON object or array into the left panel, or click Example to load a sample.
  2. Optionally change the root schema name (default: Root).
  3. Click Generate.
  4. The right panel shows the complete Zod schema with all imports and the z.infer<> type export.
  5. Copy the code and paste it into your TypeScript project.

FAQ

What is Zod?

Zod is a TypeScript-first schema declaration and validation library. You define a schema once and Zod infers the static TypeScript type automatically via z.infer<typeof schema>. It is widely used in Next.js projects, tRPC, React Hook Form, and any TypeScript codebase that needs runtime validation with full type safety.

How are JSON types mapped to Zod validators?

The generator maps JSON value types to Zod validators: JSON strings → z.string(), JSON integers → z.number().int(), JSON decimals → z.number(), JSON booleans → z.boolean(), JSON null → z.null(), JSON arrays → z.array(itemSchema), and JSON objects → z.object({...}). Null values on fields add .nullable() to the field's schema.

How are nullable fields handled?

If a JSON field is null, the generator wraps the inferred type with .nullable() — for example, z.string().nullable(). If the same key appears as both a string and null across multiple array items, it becomes z.string().nullable(). For unknown types, z.unknown() is used.

How are nested objects handled?

Each nested JSON object generates a separate named schema variable (e.g., AddressSchema) that is declared before the root schema. The root schema then references it by name. This keeps the generated code readable and allows you to reuse sub-schemas independently.

What does z.number().int() mean?

Zod's z.number() accepts any JavaScript number including decimals. Adding .int() restricts it to integers only, rejecting values like 3.14. The generator uses z.number().int() when the JSON value is a whole number, and z.number() when it has a decimal component.

How do I add optional fields to the generated schema?

The generator marks fields as optional (appends .optional()) if they are absent in some array items but present in others. For root object fields, all keys present in the example are required by default — add .optional() manually to any field that may be absent in real data.

Can I use this with tRPC or React Hook Form?

Yes. Zod schemas are the standard input validation format for tRPC procedures and React Hook Form's zodResolver. Paste the generated schema directly into your tRPC router as the input validator, or pass it to zodResolver(schema) in React Hook Form.

Do I need to install Zod separately?

Yes. The generated code imports from the "zod" package. Install it with: npm install zod or pnpm add zod. Zod requires TypeScript 4.5+ with strict mode enabled. The generated output is compatible with Zod v3 (the current stable version).