Developer Tools

JSON to TypeScript

Turn representative JSON data into a starting TypeScript type. Combine several samples to reveal fields that can be missing, values that can be null, and different element types within arrays.

Free online tool

Loading tool…

Types describe the supplied samples, not every valid API response. Review optional fields and runtime validation separately.

Object shapes merge across samples and array items. Missing properties become optional; explicit null remains null in a union. Input is processed in your browser.

Sample data is parsed and inferred locally in your browser without an API request.

GET STARTED

How to use the JSON to TypeScript

  1. 01

    Paste one JSON document, or choose multiple-sample mode and provide a top-level array containing examples of the same value.

  2. 02

    Choose a valid root type name. Select a root interface or a type alias, and enable readonly output if useful.

  3. 03

    Review the inferred properties and warnings. Copy the declarations or download them as a .ts file.

Examples

Find optional fields across records

In multiple-sample mode, [{"id":1,"name":"Ada"},{"id":2}] produces an optional name property because one record omits it. In one-document mode, the same input produces an array type.

Keep null separate from missing

Samples containing {"contact":{"email":"ada@example.com"}} and {"contact":null} produce an object-or-null union. A field is optional only when an object sample omits it.

Merge array item shapes

An array containing {"x":1} and {"y":true} yields an item shape with optional x and y properties. A mixed array such as [1,"two"] yields Array<string | number>.

Inference rules

Strings, numbers, booleans, and null become their corresponding TypeScript types. Nested object shapes merge recursively. Arrays use the union of observed item types rather than fixed-length tuples. An empty array uses unknown until a populated sample supplies more evidence.

Interfaces, aliases, and readonly output

A root object can be declared as an interface. Primitive values, arrays, unions, and empty objects use type aliases. The readonly option marks known object properties readonly and generates ReadonlyArray for arrays. These declarations do not freeze values at runtime.

Review the data contract

Sample inference cannot discover every possible response. Add examples with absent fields, empty collections, nulls, and alternate shapes. Dates remain strings, and object samples are merged into broad shapes rather than inferred as discriminated unions. Add runtime validation separately if incoming data is untrusted.

What to know

  • Strict JSON only; duplicate object keys are rejected. Inputs are limited to 2 million characters and 128 nested levels.
  • Empty objects use Record<string, unknown>. Enum literals, tuples, named nested interfaces, and JSON Schema generation are not inferred.

Common questions

Why is a property optional?

At least one object sample at that level omitted the property. Null is a present value, so it adds null to the type rather than making the property optional.

Does this generate runtime validation?

No. TypeScript declarations describe values for static checking. They do not validate a fetched response or transform data at runtime.

What happens with large numeric IDs?

JSON numeric values infer as number. The parser preserves their digits while reading samples, but JavaScript number values can lose large-integer precision. Use string identifiers in your actual data contract when precision matters.

Can I use spaces or punctuation in property names?

Yes. Every property name is emitted as a quoted TypeScript property. The root type name must be a nonreserved ASCII identifier.

Sources & references