Skip to content
All tools

JSON to TypeScript Converter

Paste JSON and get TypeScript interfaces or a Zod schema. Optional fields and unions are inferred from every array item. Nothing leaves your browser.

How it works

The converter walks the parsed JSON and builds a shape for every value: string, number, boolean, null, an array of something, or an object with fields. Each nested object gets its own named type, called after its key in PascalCase, so address becomes Address. Array items get the singular form: posts gives Post, categories gives Category.

Arrays are read item by item

One sample object tells you little about an API. An array of them tells you more, so all items are merged into one type:

  • A key missing from some items becomes optional. If only one post has coverUrl, you get coverUrl?: string.
  • Different kinds under one key become a union. code: 503 in one item and code: "ETIMEDOUT" in another give code: number | string.
  • null is kept as | null, not turned into optional. A field that is present and null is not the same as a field that isn't there, and JSON.stringify keeps the first but drops the second.
  • Empty arrays are unknown[]. There is nothing to infer from. Paste a sample where the array has items, or fix the type by hand.

Objects with exactly the same fields share one type. In the Config preset, primary and replica are both Primary. Rename it to something like DatabaseHost once you paste it into your code.

interface or type?

For plain object shapes they behave the same, so pick what your codebase uses. The practical differences: interfaces with the same name merge (declaration merging), and only type can name a union or a primitive. That's why a root that is an array is always written as type Root = Item[].

Types vs a Zod schema

A type disappears when TypeScript compiles. Writing const user = (await res.json()) as User is a promise to the compiler, and nothing checks it at runtime. If the API changes, the code still compiles and breaks somewhere later, far from the cause.

A Zod schema is a runtime check. z.infer derives the type from it, so there's one source of truth:

ts
import { z } from 'zod'; const UserSchema = z.object({ id: z.number(), email: z.string(), avatarUrl: z.string().nullable(), }); type User = z.infer<typeof UserSchema>; const result = UserSchema.safeParse(await res.json()); if (!result.success) { console.error(result.error.issues); } else { result.data.email; // typed as string }

Use parse when bad data should throw, and safeParse when you want to handle it. Schemas are emitted in dependency order, since a const can't be used before it's declared. The output works with Zod 3 and Zod 4.

What a sample can't tell you

The result describes the JSON you pasted, not every response the API can return. Tighten it by hand where you know more:

  • A string that is always one of a few values should be a union of literals ('admin' | 'editor') or z.enum([...]).
  • Dates arrive as strings. In Zod, z.coerce.date() turns them into Date objects.
  • A number that must be whole can get .int(), an email field z.email() in Zod 4 or z.string().email() in Zod 3.
  • An object used as a dictionary ({ "en": ..., "de": ... }) is better typed as Record<string, T> than as a fixed set of keys. Empty objects are already written that way.