N
NJHTools

JSON to Zod Schema

Paste JSON and get a Zod schema that validates the same shape at runtime, with TypeScript types available through z.infer. Free, no signup, and generated locally in your browser.

Language

16 lines · processed in your browser, nothing is uploaded

Zod output · root.schema.ts

Generated code appears here.

Example: JSON to Zod

This JSON sample, with the root type named User, produces the Zod below using the default options.

Input JSON
{
  "id": 1024,
  "name": "Ada Lovelace",
  "email": "ada@example.com",
  "active": true,
  "score": 98.5,
  "tags": [
    "admin",
    "beta"
  ],
  "address": {
    "city": "London",
    "postcode": "NW1 6XE"
  }
}
Generated Zod
import * as z from "zod";


export const AddressSchema = z.object({
    "city": z.string(),
    "postcode": z.string(),
});
export type Address = z.infer<typeof AddressSchema>;

export const UserSchema = z.object({
    "id": z.number(),
    "name": z.string(),
    "email": z.string(),
    "active": z.boolean(),
    "score": z.number(),
    "tags": z.array(z.string()),
    "address": AddressSchema,
});
export type User = z.infer<typeof UserSchema>;

How to convert JSON to Zod

  1. 1Paste a representative JSON sample into the input.
  2. 2Select Zod as the output language.
  3. 3Name the root schema, for example User.
  4. 4Copy the schema into your project, which needs the zod package installed.
  5. 5Call schema.parse() or safeParse() on incoming data.

One schema, both runtime checks and types

A Zod schema is a value, so it exists at runtime and can reject bad data. Derive the static type from it with type User = z.infer<typeof UserSchema> instead of maintaining a separate interface.

This keeps validation and types in sync: change the schema and every usage of the inferred type updates with it.

parse vs safeParse

schema.parse(data) returns typed data or throws a ZodError. schema.safeParse(data) never throws and returns an object with a success flag, which is easier to handle in request handlers and form code.

Validate at the boundaries of your app, such as fetch responses, webhook bodies and localStorage reads, rather than deep inside business logic.

Refining the generated schema

The generator infers basic types from your sample. Add the rules JSON cannot express, such as .email(), .min(1) or z.enum([...]) for fields that only take fixed values.

Fields missing from some records are generated as optional. If a field may be null, check that it uses .nullable(), which is different from .optional() in Zod.

Frequently Asked Questions

How do I get a TypeScript type from the Zod schema?↓

Use z.infer: type User = z.infer<typeof UserSchema>. The type is computed from the schema, so you never write the interface by hand.

Why use Zod instead of a plain TypeScript interface?↓

Interfaces are erased at compile time and cannot catch a malformed API response. A Zod schema runs at runtime and fails loudly when data does not match.

How are optional fields detected?↓

Types are inferred from the sample you paste. A field becomes optional only if it is missing from some of the objects, so paste an array with several representative records, or mark the field optional by hand after generating.

Does it handle nested objects and arrays?↓

Yes. Nested objects become their own schemas and arrays use z.array(). Reused shapes are merged so the output stays compact.

Is my JSON uploaded to a server?↓

No. The code generator (quicktype) runs entirely in your browser, so the JSON you paste never leaves your device. That makes it safe to use with internal API responses, though you should still strip secrets before sharing generated code.

Last updated: