JSON to TypeScript

Paste a JSON response and get TypeScript interfaces or types. Arrays of objects are merged so optional and nullable fields are found, nested objects get their own names, and broken JSON is fixed in one click.

JSON
TypeScript
Waiting for JSON

What you get

{ "id": 42, "name": "Ada", "avatar": null,
  "projects": [ { "id": 1, "title": "Engine" },
                { "id": 2, "title": "Notes", "archived": true } ] }
export interface Root {
  id: number;
  name: string;
  avatar: null;
  projects: Project[];
}

export interface Project {
  id: number;
  title: string;
  archived?: boolean;
}

Each nested object becomes its own named type, based on its key (projects becomes Project). Keys that aren’t valid identifiers, like created-at, are quoted.

How the types are inferred

  • Arrays of objects are merged: a key present in every item is required, one missing from some is optional.
  • Mixed values become unions, like string | number, and null in some items gives string | null.
  • A field that’s only ever null is typed null. Paste a sample where it has a value to get the real type.
  • Empty arrays become unknown[].
  • Identical shapes are reused rather than duplicated.

Notes under the output tell you when any of these guesses were made, so you know which types to check by hand.

Broken JSON?

The input uses the same checker as the JSON formatter. Trailing commas, single quotes, unquoted keys, comments and missing brackets are marked where they are and fixed in one click, so you can paste straight from a JavaScript object or a log line.

Tips

  • Paste a response with several items in its arrays to get accurate optional fields.
  • Strings that hold dates are typed string; JSON has no date type.
  • Turn on readonly for data you treat as immutable, like API responses.
  • Rename the root type to match your endpoint, such as UserResponse.

Need the same data in another format? Use JSON to YAML or JSON to CSV.

Questions people ask

How are optional fields detected?
From arrays. When an array holds several objects, they’re merged into one type, and any key that’s missing from some of them is marked optional with ?. The more examples you paste, the more accurate the types.
Should I use interface or type?
Either works for describing JSON. Interfaces can be extended and merged and give slightly clearer error messages; type aliases can express unions and mapped types. Pick whichever your codebase already uses.
What does the “| undefined” option do?
It writes optional keys as key?: T | undefined. You need this if your tsconfig has exactOptionalPropertyTypes turned on and you sometimes set those keys to undefined explicitly.
Why is an array typed unknown[]?
Because it was empty in your sample, so there’s nothing to infer an item type from. Add one example item to the array and regenerate.
Are the types checked at runtime?
No. TypeScript types disappear when your code is compiled. If the data comes from an API you don’t control, validate it at runtime too, with a schema library such as Zod or Valibot.
↑↓ move↵ openesc close