JSON to TypeScript

Generate TypeScript interfaces from a JSON sample, inferring types from the values and naming nested shapes.

7Top-level keys
YesValid JSON

Everything you type here is processed on your own device. No input is sent to a server, logged, or stored.

What the JSON to TypeScript does

This JSON to typescript will generate TypeScript interfaces from a JSON sample, inferring types from the values and naming nested shapes. Paste your text in, pick the options you need, and copy the result straight back out. It is most often used for bootstrapping types for an API response you are consuming. The companion to this one is our URL parser.

How to use it

  1. Paste a representative JSON sample.
  2. Set the root interface name.
  3. Review the generated interfaces, particularly any unknown or null fields.
  4. Widen the types by hand where the sample did not show every possibility.

How it works

Types are inferred from one sample, which is the fundamental limitation. A field that happens to be a number in this sample but can also be a string will be typed as number, and the mismatch surfaces at runtime rather than compile time. Generate from a sample that exercises the edge cases, then widen by hand.

Null is the clearest example. A field that is null in the sample tells you only that it is nullable, not what it holds when populated. Marking those optional and typed as unknown forces you to decide rather than silently accepting the wrong type.

Arrays are typed from the first element, with a check that the rest match. A heterogeneous array falls back to unknown[], which is correct and deliberately inconvenient — a mixed array usually means the shape needs a union type that only you can define.

For an API you control, generating types from an OpenAPI or JSON Schema document is strictly better than inferring from samples, because the schema states what is optional and what the unions are.

When to use it

  • Bootstrapping types for an API response you are consuming.
  • Getting a starting point for a type definition from example data.
  • Documenting the shape of a config file.
  • Checking what shape a JSON blob actually has.

Common mistakes to avoid

  • Trusting inferred types without reviewing them. One sample cannot show optionality or unions.
  • Generating from a sample where optional fields happen to be present.
  • Using inferred types where an OpenAPI schema is available, which states optionality explicitly.

Frequently asked questions

Can this infer optional fields?

Not reliably. A field present in the sample is typed as required. Optionality has to come from a schema or from your knowledge of the API.

What happens with a null value?

It is marked optional and typed unknown, because null tells you the field is nullable but nothing about what it holds when set.

Should I use this or generate from OpenAPI?

OpenAPI, whenever it exists. A schema states optionality and unions; a sample can only show what happened to be in it.

Related tools

See all developer tools →