DEVELOPER GUIDE

Generate a TypeScript type from a JSON sample

Create a starting type for an API response, then review empty arrays, optional fields, unions, and runtime validation.

5 min read•DevToolbox editorial

Paste a representative response

The generator reads a JSON value and infers TypeScript types from its observed strings, numbers, booleans, objects, and arrays. Choose a valid root name such as ApiResponse. A sample is useful for drafting a type, but it is not a complete API contract.

Input:  {"id":42,"active":true}
Output: export type ApiResponse = {
          id: number;
          active: boolean;
        };

Review arrays and nested objects

An empty array becomes unknown[] because no element type can be inferred. An array with different observed values becomes a union of those types. Nested objects remain nested object types, and unusual property names are quoted so the output stays valid TypeScript.

Add optional fields yourself

This converter describes the values in the sample you provide. It does not merge multiple object examples into a single shape or infer which properties a future response may omit. Compare real API responses or consult the API schema, then add optional markers and wider unions where needed.

Types do not validate incoming data

TypeScript types disappear when code runs. A generated type does not check a network response or prove that an API follows the example. Validate untrusted data at runtime and handle unexpected fields or missing values before using them.

Put it into practice with a free tool that runs in your browser.
Open JSON to TypeScript