JSON to Zod
Generate Zod schemas from JSON data.
How to Use
- Paste your JSON into the input panel. The tool accepts both objects and arrays — for arrays, the first element's shape is used to infer the schema.
- A Zod schema is generated automatically. Each JSON type maps to a Zod validator: strings become z.string(), numbers become z.number(), booleans become z.boolean(), null becomes z.null(), and nested objects become z.object(...).
- Copy the generated schema and paste it into your TypeScript project. Use schema.parse(data) for strict validation or schema.safeParse(data) for error-safe parsing without throwing.
Frequently Asked Questions
- How are types inferred from JSON values?
- The tool maps JSON primitives directly: string to z.string(), integer to z.number().int(), float to z.number(), boolean to z.boolean(), null to z.null(). Arrays use z.array() with the inferred element schema.
- How do I use safeParse instead of parse?
- Replace schema.parse(data) with schema.safeParse(data). It returns an object with success: true (plus the typed data) or success: false (plus a ZodError), instead of throwing — which is safer for validating untrusted user input.
- Can I mark fields as optional?
- Generated fields are all required by default. Append .optional() to any field schema to make it optional — for example, change z.string() to z.string().optional() for fields that may be absent in the data.