All tools Developer

JSON to TypeScript

Generate TypeScript interfaces from JSON, with nested types, unions and optional fields inferred.

Load example

Runs in your browser. Nothing you open or type here is uploaded.

How to use

  1. Paste your JSON into the input field.
  2. Set the root interface name.
  3. Generate, then read the TypeScript interfaces in the output.
  4. Copy the interfaces into your project.

Details

The tool reads a JSON value and infers the TypeScript types that describe it: strings, numbers, booleans, nested objects and arrays. Nested objects become their own interfaces. When a value is an array of objects, the objects are merged into one interface; a key that is missing from some array elements becomes an optional property, and an array holding mixed types becomes a union, such as (string | number)[]. Keys that are not valid JavaScript identifiers are kept but quoted, so the output compiles as written.

  • Empty arrays become unknown[], because there is no element to infer a type from. Add one example element to get a concrete type.
  • The generated types describe the sample you pasted. Fields that never appear in the sample, and shapes that only occur in other responses, cannot be inferred.

Common questions

How are optional fields decided?

When you convert an array of objects, a key that appears in some objects but not all of them becomes optional (key?: type). Keys present everywhere stay required.

Does my JSON leave the browser?

Never. All inference runs locally in your browser; nothing is uploaded.

What happens with unusual keys?

Keys that aren't valid JavaScript identifiers (like "user-name" or "123") are kept as-is but quoted, so the output always compiles.

How are arrays typed?

Arrays get the union of their element types, e.g. (string | number)[]. Arrays of objects are merged into a single interface, and empty arrays become unknown[].