JSON to TypeScript

Infer TypeScript interfaces from a JSON sample.

About JSON to TypeScript

Generate TypeScript interfaces from a JSON sample. Paste JSON and a typed interface appears as you type, with a named interface for every nested object.

How to use

  1. Paste JSON, or load a .json file (up to 5 MB) with the file picker or by dropping it.
  2. Optionally change the root interface name (default Data, up to 64 characters).
  3. The TypeScript updates as you type. Copy it, or Download it as a .ts file.

Inference rules

  • string, number, boolean and null map to the matching TypeScript type. A field that is null in some array items and set in others becomes T | null.
  • A nested object becomes its own interface named after its path, for example DataAddress or DataUserAddress, so two address objects under different parents keep their own shapes. Nested interfaces are listed before the interface that uses them.
  • An array takes the element type: string[] when all items match, a union such as (string | number)[] when they differ, and any[] when the array is empty.
  • An array of objects, including a top-level array, is typed from all its items: a key missing from some items gets the optional ? marker.
  • Interfaces are not exported. A key that is not a valid identifier is quoted, as in "first-name": string.

Example

{ "id": 1, "name": "Ada", "tags": ["admin"] }
→
interface Data {
  id: number;
  name: string;
  tags: string[];
}

Privacy

Inference runs in the page. No request is made and nothing is stored.

Frequently asked questions

How do I generate a TypeScript interface from a JSON response?
Paste a representative response into a JSON-to-TypeScript tool; it infers a type for each key from the value in the sample. Check the result for fields that can be null or missing, since a single sample cannot show that.
What is the difference between interface and type in TypeScript?
Both describe object shapes. An interface can be extended and merged across declarations, while a type alias can also name unions, tuples and primitives. For data shapes from JSON either works; this tool emits interfaces.
Why is an empty array typed as any[]?
An empty array has no elements to infer from, so there is no evidence of what it holds. Replace any[] with the real element type once you know it.
Why are nested objects separate interfaces instead of inline types?
Named interfaces can be reused and referenced in function signatures, and they keep the root interface short. The generated names follow a RootKey pattern so they can be renamed with a single search and replace.