Skip to content
DevTrove

JSON & Data

JSON to TypeScript

Generate TypeScript interfaces from a JSON sample, with optional properties and union types.

Runs in your browser

Press Ctrl+Enter (⌘+Enter on Mac) to generate.

How to use

  1. Paste a JSON object or array — for example an API response — into the input box.
  2. Select Generate, or press Ctrl+Enter (⌘+Enter on Mac) in the input box.
  3. Review the generated interfaces; the top-level type is always named Root.
  4. Use Copy output to copy the TypeScript into your code.

How JSON values map to TypeScript

Strings become string, numbers become number, true and false become boolean, and null stays null — a value that is only ever null is never guessed to be a string. Every object becomes an exported interface, and arrays become T[]. An empty array becomes unknown[], because a sample with no items says nothing about their type; unknown is used instead of any so the compiler still checks how you use it.

When one position holds different kinds of values, the type is a union in the order the kinds first appear: [1, "x", null] becomes (number | string | null)[]. Number values themselves are never copied into the types, so large or precise numbers in your sample cannot be altered.

Arrays of objects and optional properties

All objects in the same place — for example every item of a users array — are merged into one interface. A property that appears in every object is required; a property missing from some objects is marked optional (email?: string). If a property holds different kinds of values in different objects, its type is a union. Nothing is dropped: every key and every observed kind appears in the output.

Interfaces with exactly the same properties and types are generated once and reused, so repeated structures such as an author and an editor with the same fields share one type. An object with no properties becomes Record<string, unknown>.

Names and property keys

The top-level type is named Root. Other interface names come from the property key in PascalCase — address becomes Address and user-name becomes UserName. Items of an array are named after the singular form (users → User, categories → Category) or with an Item suffix (data → DataItem). Names that would start with a digit get a Type prefix, names of built-in types such as Date or Record get a Type suffix, and when two different interfaces would share a name, the later one gets a number (Address, Address2).

Property keys are never renamed or re-cased. Keys that are not plain identifiers — such as "first name", "user-name" or "123abc" — are written in quotes, exactly as in your JSON.

Strict input and limits

Input must be valid JSON (RFC 8259) and is never repaired: comments, trailing commas and single quotes are reported with their line and column. An object that repeats a key is rejected, because JSON parsers keep only one of the values and the generated type would hide the other.

Input is limited to 5,000,000 characters, nesting to 100 levels and output to 20,000,000 characters. Output is never truncated: if it would be larger, nothing is generated.

Types from a sample, not runtime validation

This tool infers static types from the sample you provide. It cannot know about values your sample does not contain, and TypeScript types are not checked at runtime, so they do not validate data your application receives later. Review the result — for example, a property that happened to be null in the sample may hold other values in real data.

FAQ

Is my JSON uploaded?
No. Your JSON is processed in your browser. It is not sent to a server, not stored, and not added to the page URL.
Why does an empty array become unknown[]?
An empty array shows no items, so its item type cannot be inferred. unknown[] is honest about that and still type-safe; add an example item to your sample to get a precise type.
Why not any?
any switches off type checking. The generator only produces precise types, unions and unknown, so the compiler keeps checking your code.