JSON to Zod Schema

Infer Zod schemas and their TypeScript types from a JSON sample.

About JSON to Zod Schema

Generate Zod schemas and their inferred TypeScript types from a JSON sample, with one z.object for each nested object, as you type.

How to use

  1. Paste JSON, or load a .json file (up to 5 MB). The root must be an object or an array of objects.
  2. Optionally change the root type name (default Root). It must start with a letter or underscore and contain only letters, digits and underscores.
  3. The schema updates as you type. Copy it, or Download it as a .ts file. The output imports z from the zod package.

What it does

JSON Zod
string z.string()
YYYY-MM-DD string z.string().date()
date-time string with T, seconds and a zone z.string().datetime({ offset: true })
integer z.number().int()
other number z.number()
boolean z.boolean()
array z.array(T)
nested object its own schema, RootAddressSchema
mixed types z.union([A, B])
only null, or the items of an empty array z.unknown()
null in some items .nullable()
key missing from some items .optional(), or .nullish() when it is also null somewhere
  • Each object becomes export const NameSchema = z.object({ ... }) followed by export type Name = z.infer<typeof NameSchema>. Nested schemas are listed before the schema that uses them.
  • A date-time is refined only when every sample has T, seconds and either Z or a +hh:mm offset, which is what Zod's check accepts. Other layouts, such as 2024-01-31 10:00, stay z.string().
  • Keys that are not identifiers, such as first-name, are quoted.
  • Integers above 2^53 are typed z.number(): JavaScript cannot hold them exactly, and Zod 4 rejects them in .int().
  • It does not infer enums, string lengths, number ranges, email or URL formats, or records with arbitrary keys, because one sample cannot show them. z.object strips unknown keys; add .strict() yourself to reject them.
  • The .date() check needs Zod 3.23 or later.

Example

{ "id": 1, "name": "Ada", "joined": "2024-01-31" }
import { z } from 'zod';

export const RootSchema = z.object({
  id: z.number().int(),
  name: z.string(),
  joined: z.string().date(),
});
export type Root = z.infer<typeof RootSchema>;

Privacy

Inference runs in the page. No request is made and nothing is stored.

Frequently asked questions

How do I get a TypeScript type from a Zod schema?
Use z.infer: type Root = z.infer<typeof RootSchema>. The type is derived from the schema, so the two cannot drift apart. The generator writes this line after every schema.
What is the difference between optional, nullable and nullish in Zod?
.optional() accepts undefined, which for an object key means the key may be missing. .nullable() accepts null. .nullish() accepts both.
How do I validate a date string with Zod?
z.string().date() accepts a YYYY-MM-DD date and z.string().datetime() an ISO 8601 date-time ending in Z; pass { offset: true } to also accept offsets such as +05:30. Both leave the value a string. Use z.coerce.date() to get a Date object instead.
Does Zod reject unknown keys?
Not by default: z.object() removes keys the schema does not declare and returns the rest. Call .strict() on the object schema to make an unknown key a validation error.