Skip to content

JSON to TypeScript - Generate Interfaces from JSON

Input JSON
TypeScript Output

From an API response to types

Most people reach for this when a new endpoint lands and the only documentation is a response body. Paste the JSON on the left and you get one TypeScript declaration per object shape on the right, named after the key that holds it. Keys that aren't valid identifiers, such as "+1" or "first-name", are kept as quoted property names so the type still matches the payload.

Example: a GitHub-style issue

Input, with the root type name set to Issue:

{
  "id": 4211,
  "title": "Fix login redirect",
  "labels": ["bug", "auth"],
  "assignee": null,
  "author": { "login": "ada", "site_admin": false },
  "comments": [
    { "id": 1, "body": "Repro steps attached", "edited_at": null },
    { "id": 2, "body": "Fixed in #4212", "reactions": { "+1": 3 } }
  ]
}

Output:

export interface Issue {
  id: number;
  title: string;
  labels: string[];
  assignee: null;
  author: Author;
  comments: Comment[];
}

export interface Comment {
  id: number;
  body: string;
  edited_at?: null;
  reactions?: Reactions;
}

export interface Reactions {
  "+1": number;
}

export interface Author {
  login: string;
  site_admin: boolean;
}

The two comments are merged into a single Comment interface. reactions is only on the second one, so it gets ?. edited_at is only on the first one and only ever null, so it comes out as edited_at?: null. That is accurate for this sample and wrong for your app, and the same goes for assignee. Fix those by hand or paste a richer sample. Property names are not renamed: site_admin stays snake_case because that is what arrives over the wire.

Options

Declare as switches every declaration between interface Author { } and type Author = { };. Export adds or removes the export keyword. Readonly prefixes each property with readonly, which suits data you only read from a cache or store. All properties optional puts ? on everything, handy for a PATCH body; for the same type in both forms you can also keep it off and use Partial<Issue>.

What it won't guess

Numbers are always number, even for IDs too big for a JavaScript double. A string like "open" stays string rather than a literal union, because three samples are not enough to know the full set of values. Date strings stay string too, since JSON.parse never gives you a Date. Objects with no keys become Record<string, unknown>.

If you turn on exactOptionalPropertyTypes in your tsconfig, reactions?: Reactions no longer accepts an explicit undefined. That matches JSON, which has no undefined, so the generated types work under that flag without changes.

Types are compile-time only. To check a response at runtime, use the JSON to Zod page with the same input, or create a JSON Schema if another service or language needs to share the contract.

Same JSON, other languages

Every generator here reads the sample the same way, so a key that is optional or nullable in one language is optional or nullable in the others. Paste the same input into: Zod, Go, Python, Rust, Java, C#, JSON Schema.

Related Tools

Frequently Asked Questions

Why is a field typed as just `null`?
Because null is the only value the generator saw for it. It has no way to know that `assignee` will be a user object on another record. Either paste a record where the field is filled in, or edit the type to something like `User | null` yourself.
What is the difference between `email?: string` and `email: string | null`?
`?` means the key can be missing from the object. `| null` means the key is there but its value is null. The generator uses `?` when a key is absent from at least one array item and `| null` when it saw a null value. A field can get both.
Should I pick interface or type alias?
For plain object shapes from an API they behave the same. Interfaces can be extended with `extends` and merged by redeclaring them; type aliases can't be reopened but handle unions and mapped types. Most codebases pick one and stick to it, so use whatever your lint rules expect.
Do these types check the data at runtime?
No. TypeScript types are erased when you compile, so a response that doesn't match will still get through. If the data comes from outside your code, generate a Zod schema from the same JSON and parse the response with it.
Why do empty arrays become `unknown[]`?
An empty array has no items to look at. `unknown[]` forces you to narrow the type before you use an item, which is safer than `any[]`. If another record in your sample has items in that array, their type is used instead.