Skip to content

JSON to TypeScript Interface & Type Generator

Generate TypeScript interfaces or type aliases from a JSON sample, such as an API response. Nesting, null and mixed arrays are handled, and the conversion runs in your browser.

Create named TypeScript types for an API response.

Converted in your browser. Null stays a value type; ? marks a field missing from some objects in an array. Empty arrays use unknown elements.

Load an example (replaces your input)

JSON

TypeScript

When to use it

  • Create named TypeScript types for an API response.
  • Compare several objects in an array to see differing types and optional fields.

How to use

  1. Paste real JSON or load one of the examples. Loading an example replaces your input.
  2. Set the root type name and choose interface (nested objects become separate types) or type (one type alias with nested objects inline).
  3. Check null, optional (?) fields and string/number unions, then copy the types.

JSON input

[{"id":1,"name":"Tokyo"},{"id":2}]

TypeScript output (Root, interface)

export type Root = RootValueItem[];

export interface RootValueItem {
  id: number;
  name?: string;
}

Details and limitations

Every array element is inspected. In the example, name is missing from the second object, so it becomes optional; a field that is present with null becomes a union with null instead.

An empty array gives unknown[]. Mixed arrays become arrays of a union type, not fixed-length tuples.

Types only cover what appears in the sample. Fields that never appear, date or email formats, value ranges and relationships between fields are not inferred.

Input must be standard JSON; clean up comments or trailing commas with the JSON formatter first. Enter IDs with leading zeros as strings.

Input is limited to 1,000,000 characters, about 64 levels and 50,000 values. TypeScript types do not validate data at runtime.

FAQ

Should I generate an interface or a type?

Both describe the same shape. interface creates one named interface per nested object, which is easy to reuse; type creates a single type alias with nested objects written inline.

Can it generate a TypeScript class?

No, it generates interfaces or type aliases only. If you need runtime validation, use JSON to Zod and adjust the schema to your rules.

What is the difference between null and an optional field?

{"name":null} has a name whose value is null; {} has no name at all. null stays in the type, and ? is added only when some objects in the same array lack the field.

What does a JSON array at the root produce?

A type alias for the array plus an interface for its items, as in the example above, where Root is RootValueItem[].

Specifications and references