toolqik

135 · JSON

JSON to TypeScript

Generate TypeScript interfaces or types from example JSON, with optional and null fields.

Runs in your browser
Declare as
Unknown types
Options
TypeScript code

  

About this tool

Typing an API response by hand is slow and easy to get wrong. Paste a JSON example and this tool writes the TypeScript for you: an interface (or type) for every object, arrays typed by their items, optional properties for fields that aren’t always present, and | null for fields that can be null.

Example

{ "id": 7, "tags": ["a"], "address": { "city": "Pune" }, "manager": null }

export interface Root {
  id: number;
  tags: string[];
  address: Address;
  manager: null;
}

export interface Address {
  city: string;
}

interface or type?

For object shapes both work. interface gives clearer error messages and can be extended or merged; type is needed for unions and mapped types. Many codebases pick one style for consistency; the TypeScript handbook leans towards interface for object shapes.

unknown or any?

When a field holds mixed types, the generator can’t pick one. unknown (the default) forces you to check the type before using the value, which catches bugs; any switches type checking off for that field.

How types are inferred

  • Every object becomes its own named type. Names come from the key (address → Address), and items of an array get the singular (orders → Order).
  • For arrays of objects, all items are merged into one type: a field missing from some items becomes optional, and a field that is sometimes null becomes nullable.
  • Integers and decimals in the same field merge to a decimal type. Fields with mixed types (say a number in one item and a string in another) fall back to the language’s “any” type.
  • Identical nested shapes are reused instead of generating duplicates.

The code is generated from examples, so it can only be as accurate as the sample. Paste a response that includes optional fields and nulls (or several items) for the best result, then check the types against the API documentation. Generated in your browser: your JSON is not uploaded.

Types are not validation

TypeScript types disappear at runtime: const user = (await res.json()) as Root trusts the server completely. For data you don’t control, validate it at the boundary with a schema library such as Zod, Valibot or Ajv. You can create a JSON Schema from the same sample with the JSON Schema generator.

FAQ

How do I generate a TypeScript interface from JSON?

Paste the JSON above. An interface is generated for the root object and every nested object, with array and primitive types filled in. Copy or download the .ts file.

How are optional fields detected?

From arrays of objects: if a key is missing in some items, it is marked optional with ?. Turn on “Top-level array = examples of one object” to merge several sample objects into one type.

How does it handle null values?

A field that is null in some samples becomes a union such as string | null. A field that is only ever null is typed null; replace it with the real type from your API docs.

Should I use interface or type?

Either works for object shapes. interface is the common default and gives clearer errors; type is needed for unions and mapped types.

Are keys with dashes or spaces supported?

Yes. Keys that are not valid identifiers are quoted, for example "postal-code": string.

esc
↑↓ navigate↵ open