Code generator

JSON to TypeScript Interfaces

Paste a JSON example, such as an API response, and get ready-to-use TypeScript interfaces for every nested object.

How to generate TypeScript types

  1. Paste a real example of the JSON, for example a response from your API.
  2. Set the name of the root type. Nested objects get names from their keys, such as Profile or Repo.
  3. Choose interface or type, and whether null fields should be optional or readonly.
  4. Copy the result or download it as a .ts file.

How types are inferred

  • Arrays of objects are merged. If items have different keys, every key is included, and keys missing from some items are marked optional with ?.
  • Mixed values become unions. A field that is a string in one item and null in another becomes string | null.
  • Empty arrays become unknown[], since there is nothing to infer from. Add one example item for a precise type.
  • Keys that aren't valid identifiers, like created-at, are quoted automatically.

Tips

The output is only as good as the example. Paste a response that includes optional fields and several array items, so the generator sees every variation. Date strings are typed as string, because JSON has no date type. Convert them with new Date() when you use them.

Frequently asked questions

Should I use interface or type?

Both work for describing JSON. Interfaces can be extended and merged, and give clearer error messages. Types can express unions and aliases. Many teams pick one style and use it everywhere.

Why is a field marked optional?

It was missing from at least one object in an array, or you turned on the option to make null fields optional.

How are large numbers typed?

As number, because JSON numbers map to TypeScript number. If your IDs are larger than 2^53, consider sending them as strings from your API.

Does it validate the JSON at runtime?

No. TypeScript types only exist at compile time. Use a runtime validator such as a JSON Schema library if you need to check incoming data.

Is my data uploaded?

No. The conversion runs with JavaScript inside your browser tab. Nothing you paste or open is sent to a server or stored.