Skip to content
Toolew.com

JSON to TypeScript

Generate TypeScript interfaces or type aliases from sample JSON, with optional keys, nested types and union detection.

Runs in your browser. Your data is not uploaded.
Loading tool…

This guide is not yet available in your language and is shown in English.

Tool guide

What this tool does

JSON to TypeScript reads a sample API response or config file and writes matching TypeScript declarations. Nested objects become named interfaces, arrays of objects are merged so keys missing from some items become optional, identical shapes are reused, and you can choose between interface and type alias, readonly fields and array syntax.

Typing an API response by hand is slow and error-prone. A real payload can have dozens of keys, nested objects several levels deep and arrays whose items do not all carry the same fields. Miss one optional key and the compiler will happily let you read a value that is undefined at runtime; mark everything optional and you lose most of the benefit of TypeScript.

This generator walks your JSON and infers a type for every value. When an array holds several objects, it merges them: a key present in every item is required, a key found in only some items becomes optional (key?: T), and a key whose values differ in type becomes a union such as string | number. Nested objects get PascalCase names derived from their keys, plural array keys are singularised (items becomes Item), and objects with an identical structure share one declaration. Everything runs locally, so private payloads stay on your machine.

How to use JSON to TypeScript

  1. Paste a JSON sample into JSON sample, open a .json file, or click Sample to load an example order.
  2. Set Root type name to the name you want for the top-level type, for example ApiResponse.
  3. Choose Declare as interface or type alias, and decide how Keys missing from some items should be typed.
  4. Turn on Top-level array is a list of samples if you pasted an array of records and want one merged Root type.
  5. Adjust Readonly properties, Array syntax, Semicolons and Comment ISO date strings to match your code style.
  6. Copy the result or download types.ts. Drop several files in batch mode to convert them all at once.

Tips for better results

  • Paste several real responses wrapped in an array with the samples option on; the more examples you give, the more accurate the optional keys become.
  • A field that only ever contains null in your sample is typed as null. Add a sample where it has a value so the generator can write string | null.
  • Keys that are not valid identifiers, such as shipping-address, are quoted automatically, which keeps the output compilable.
  • Use the type alias style if you plan to combine the results with intersections or mapped types; use interface if consumers may extend them.

Common use cases

  • Typing a third-party REST API response before writing a fetch wrapper.
  • Creating types for JSON configuration files loaded at start-up.
  • Bootstrapping models for a front end from a backend team’s example payloads.
  • Documenting the shape of webhook bodies in a shared types package.

Common mistakes to avoid

  • Trusting a single example. One response rarely shows every optional field or every possible value type.
  • Treating generated types as runtime validation. TypeScript types disappear at build time, so validate untrusted input with a schema as well.
  • Leaving string where a field is really a fixed set of values; refine status: string to a union such as 'paid' | 'refunded' by hand.

Frequently asked questions

Why are some keys optional in the output?

When an array contains several objects, a key that is missing from at least one of them is marked with ?. You can switch this to a required T | undefined field or ignore it under Keys missing from some items.

How are integers and decimals typed?

Both become number, because TypeScript has a single number type for IEEE 754 doubles. Use the JSON Schema Generator if you need to distinguish integer from number.

Does it support JSON with comments or trailing commas?

Yes. The parser accepts comments and trailing commas, as found in tsconfig.json and many editor settings files, and reports the line and column of any real syntax error.

What happens to empty arrays?

An empty array gives no information about its items, so it is typed as unknown[]. Add at least one element to the sample to get a precise item type.

Feedback & suggestions

Help us improve JSON to TypeScript. We read every message.

Rating (optional)
Necessary
Required for security, language and theme preferences. Always on.