CompleteSEOTools
Home › Developer Tools › JSON to TypeScript

JSON to TypeScript

Paste a JSON sample and get interfaces you can drop straight into your project.

About JSON to TypeScript

Writing types for an API response by hand is slow and easy to get wrong. Paste a sample of the JSON and this tool reads its shape and writes the TypeScript for it. Nested objects get their own named interfaces, arrays get item types, and when the objects inside an array do not all have the same keys the missing ones are marked optional. You can pick interface or type, add the export keyword and name the root type yourself.

  • Typing a third-party API response before you write code against it.
  • Creating a model for a config file or a fixture.
  • Checking what shape a long JSON document really has.
  • Saving the repetitive part of writing client code.

How to use JSON to TypeScript

1

Step 1

Paste the JSON sample and set the root name.

2

Step 2

Choose interface or type and whether to export them.

3

Step 3

Copy the output into a .ts file.

Good to know

  • Types come from the sample you give. A field that is always present in your sample may be missing from other responses, so paste several items in an array when you can.
  • A null value is typed as null. If the real field is sometimes a string, give an example of both.
  • Date strings stay as string, because JSON has no date type.

Common questions

How does it decide a field is optional?

When the same array holds several objects and a key is missing from some of them, the key is marked with a question mark. A single object cannot show that, so all its fields are required.

What happens with an empty array?

There is nothing to learn from it, so it is typed as unknown array. Replace it with the real item type once you know it.

Does it handle keys with spaces or dashes?

Yes. Keys that are not valid identifiers are written in quotes, so the interface stays valid.