Loading JSON to Zod Converter...
Please wait a moment

JSON to Zod: Schemas Built from a Real Sample

Writing a Zod schema for a big API response by hand is slow and easy to get wrong. Paste one response here and you get a schema that already accepts it, ready to tighten with your own rules.

Step 1

Paste or upload a JSON sample

Any valid JSON works: an object, a list of objects or even a single value. The more varied your sample, the better the schema, because every item of a list is taken into account:

•
Paste: drop a response copied from your browser's network tab or an API client straight into the editor
•
Upload: pick a .json or .txt file from your computer
•
Sample: load a user profile with two posts to see how nested objects and lists come out
Step 2

Read the generated schemas

The schemas update half a second after you stop typing. A few things to know about what comes out:

•
Numbers: whole numbers get .int(), values written with a decimal point or exponent don't, and quoted digits like "00501" stay z.string()
•
Lists: all items are merged, so a key that only some items have becomes .optional() and nulls make a field .nullable()
•
Nested objects: each one gets its own exported schema and z.infer type, named after its key
•
Syntax errors: broken JSON shows the line and column of the first problem instead of a schema; the JSON Validator goes into more detail
Step 3

Copy or download schema.ts

The file starts with import { z } from "zod", so it drops into any TypeScript project that has zod installed:

•
Validate: RootSchema.parse(data) throws on bad data, RootSchema.safeParse(data) returns the problems instead
•
Types: the exported Root type comes from the schema through z.infer, so the two never drift apart
•
Frameworks: the schemas plug into tRPC procedures and React Hook Form like any hand-written Zod schema

Example: A User Record

Here is a small user record with a decimal balance, a zip code kept as text, and a nickname that happens to be null:

{
  "username": "elara_quinn",
  "email": "[email protected]",
  "age": 25,
  "balance": 14.50,
  "zip": "00501",
  "isActive": true,
  "nickname": null,
  "roles": ["admin", "editor"]
}

This is exactly what the converter writes for it. Note that balance has no .int() because 14.50 has a decimal point, and nickname is z.null() until you tell it what else it can be:

import { z } from "zod";

// Check data with RootSchema.parse(data) (throws on a mismatch) or RootSchema.safeParse(data).
// Plain z.object() drops keys it doesn't know; use .strict() to reject them instead.

export const RootSchema = z.object({
  username: z.string(),
  email: z.string(),
  age: z.number().int(),
  balance: z.number(),
  zip: z.string(),
  isActive: z.boolean(),
  nickname: z.null(),
  roles: z.array(z.string()),
});
export type Root = z.infer<typeof RootSchema>;

What is Zod and Why Use It?

Zod is a TypeScript-first validation library. TypeScript types disappear when your code compiles, so they can't stop a malformed API response or form submission at runtime; a Zod schema can, and it hands you the matching TypeScript type through z.infer. The project lives on GitHub. If you only need compile-time types, the JSON to TypeScript converter writes plain interfaces instead.

Frequently Asked Questions

How does it choose between z.number() and z.number().int()?

From the way each number is written in your JSON. Whole numbers like 42 or 1250 get z.number().int(); anything with a decimal point or exponent (14.50, 1.0, 1e3) gets a plain z.number(). If a list mixes 3 and 14.5 for the same key, the field becomes z.number() so both values pass.

What happens when a key is missing or null in some items?

Every item of a list is read, not just the first one. A key that some items leave out gets .optional(), and a key that is null somewhere gets .nullable(). A key that is null in every sample becomes z.null(), because there is nothing else to go on; change it to something like z.string().nullable() once you know the real type. Values of different kinds under one key turn into a z.union().

Why does a long ID like 8901240544102066246 get z.number() and a note?

JavaScript numbers hold whole numbers exactly only up to 9007199254740991 (2^53 - 1). Anything longer, such as ICCIDs, snowflake IDs or 64-bit database keys, is already rounded by JSON.parse before Zod ever sees it, so .int() would only check the rounded value. The schema keeps a numeric z.number() and adds a comment naming the field. If every digit matters, have the API send the value as a string and check it with z.string().

Does the generated code work with Zod 3 and Zod 4?

Yes. It only uses z.object, z.array, z.union, z.string, z.number().int(), z.boolean, z.null, z.unknown, .nullable(), .optional() and z.infer, which behave the same in both versions. Keys that are not plain identifiers, like "first-name" or "user id", are written in quotes so the object keeps the exact key from your JSON.

Will it add .email(), .url() or length checks?

No. A single sample can show that a value is a string, but not that every future value must be an email address or a URL, so the generator sticks to types. Add the rules you need by hand: z.string().email() and z.string().url() in Zod 3, or z.email() and z.url() in Zod 4, plus .min() and .max() for lengths.

Can I use the schemas with React Hook Form or tRPC?

Yes, they are ordinary Zod schemas. Pass RootSchema to zodResolver from @hookform/resolvers/zod for a form, or to .input() in a tRPC procedure. The exported Root type (z.infer<typeof RootSchema>) gives you the matching TypeScript type for the rest of your code.

Is my JSON uploaded anywhere?

No. The schemas are built in your browser and the JSON is not sent to a server or kept anywhere. Reloading the page starts again with an empty editor.