JSON to TypeScript and Zod
Turn a JSON sample into TypeScript interfaces or a Zod schema, with optional keys, nullable values and string formats worked out for you.
When an API hands you JSON and no types, writing the interfaces by hand is slow and easy to get subtly wrong. Paste a sample below and get TypeScript interfaces or a Zod schema. The JSON never leaves your browser.
JSON
TypeScript
export interface Order { id: string; number: number; status: string; placedAt: string; deliveryDate: string; total: number; currency: string; giftNote: null; customer: Customer; items: Item[]; tracking: Tracking; paid: boolean; } export interface Tracking { carrier: string; url: string; events: TrackingEvent[]; } export interface TrackingEvent { at: string; place: string; } export interface Item { sku: string; title: string; qty: number; price: number; tags: string[]; discount?: Discount; giftFor?: string; } export interface Discount { code: string; percent: number; } export interface Customer { id: number; name: string; email: string; phone: null; address: Address; } export interface Address { line1: string; city: string; postcode: string; "country-code": string; }
How the types are worked out
- Objects in an array are merged. If some items have a key and others don't, that key becomes optional:
?in TypeScript,.optional()in Zod. nullnext to a real value becomesT | null, or.nullable(). A key that is only evernullin your sample stays typed asnull, because there's nothing to say what it really holds.- Mixed arrays become unions, and empty arrays become
unknown[]. - Objects with the same shape share one named type. Names come from the keys, made singular for arrays, so
itemsgivesItem. A name that clashes with a built-in type gets its parent's name in front, soeventsinsidetrackingbecomesTrackingEventrather thanEvent. - Keys that aren't valid identifiers are quoted, like
"country-code".
Zod specifics
Schemas are written children first, so each one is defined before anything uses it, and each gets a matching z.infer type.
With format detection on, strings that look like emails, URLs, UUIDs, ISO dates or ISO date-times get the matching validator. Zod 4 moved these to top-level functions, so the two versions differ:
| Zod 4 | Zod 3 |
|---|---|
z.email() |
z.string().email() |
z.iso.datetime({ offset: true }) |
z.string().datetime({ offset: true }) |
z.int() |
z.number().int() |
z.strictObject({...}) |
z.object({...}).strict() |
Check before you ship
A sample shows what an API did send, not everything it can send. Paste a few varied items to catch optional keys. Turn off "Integers" if a number that happens to be whole in your sample, like a price of 349, could be a decimal later. Comments and trailing commas in the input are fine.