JSON to Zod Schema: Runtime Validation for TypeScript
Zod is the de-facto standard for runtime type validation in TypeScript. Paste your API response or config JSON and get a Zod schema ready to use in your Next.js server actions, tRPC procedures, or form validation. The schema also exports a TypeScript type via z.infer.
Frequently Asked Questions
What is Zod?
Zod is a TypeScript-first schema declaration and validation library. It lets you define the shape of data and validate it at runtime, generating TypeScript types automatically from the schema.
How are integers handled vs floats?
JSON integers (no decimal) become z.number().int(). Numbers with decimals become z.number(). This is inferred from the sample value — if your field can be both, adjust the schema manually.
What does null become?
JSON null becomes z.null(). If the field is sometimes null and sometimes a string, change it to z.union([z.string(), z.null()]) or z.string().nullable().
How are optional fields handled?
The generator marks all fields as required (no .optional()). Add .optional() or .nullish() manually for fields that may be absent in real data.
Are arrays typed from all elements or just the first?
Arrays are typed from the first element only. If your array contains mixed types, adjust the generated z.array() to use a union type.
Where does the z.infer<typeof schema> type come from?
Zod's z.infer utility type extracts the TypeScript type from a schema at compile time. The exported Schema type gives you full TypeScript type safety from the runtime validator.