JSON to TypeScript — generate interfaces from sample JSON

Paste a sample of your JSON and get TypeScript interfaces or type aliases, with nested objects split out and optional fields worked out from every item in an array.

Live demo · real result from this tool
Declare as

TypeScript

export interface User {  id: number;  name: string;  email: string;  active: boolean;  roles: string[];  address: Address;  lastLogin: null;} export interface Address {  city: string;  postcode: string;} 

Processed on your device — nothing is uploaded.

How to use the JSON to TypeScript converter

  1. Paste a representative sample into the JSON box, such as an API response.
  2. Enter a Root type name, for example User or Order.
  3. Under Declare as, choose interface or type.
  4. Leave Export declarations ticked to add export so the types can be imported from other files.
  5. Copy the TypeScript output or download it as types.ts.

What it does and when to use it

Writing types by hand for a large API response is slow and easy to get wrong. This converter reads real data and writes the matching TypeScript declarations: numbers, strings, booleans, arrays, nested objects and null. Use it when you start working with a new API, when you add types to an older JavaScript project, or when you want a first draft of types to refine.

Each nested object becomes its own named declaration, so address becomes Address. Keys that are not valid identifiers, such as content-type, are quoted. For an array at the root, the root name becomes an array type and the item type is named in the singular, so Users gives User[].

If the JSON will not parse, check it with the JSON validator first. To make the sample readable before converting, use the JSON formatter. Writing in Go instead? The JSON to Go converter builds structs the same way.

How it works

The JSON is parsed in your browser, then walked from the root. For each value the converter records its type. For arrays of objects, it merges the fields of every element: if a field is missing from any element it becomes optional, and if it has different types in different elements the types are joined into a union. Arrays of plain values become string[], number[] and so on. Finally it prints the declarations, root first, with two-space indentation.

Worked examples

All TypeScript below is the converter’s own output.

A single object with nesting. The JSON {"id":1,"name":"Asha","tags":["a"],"address":{"city":"Pune"},"manager":null} with root name User gives:

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

export interface Address {
  city: string;
}

manager is typed null because that is the only value the sample shows.

An array with a missing field. [{"id":1,"email":"a@x.io"},{"id":2}] with root name Users gives export type Users = User[]; and an interface User with id: number; and email?: string;. The second item has no email, so the field is optional.

Limits and tips

  • Your sample sets the limits. A field that never appears in the sample will not appear in the types. Paste several varied items if you can.
  • Dates stay strings. JSON has no date type, so "2026-10-03" becomes string.
  • Enums are not guessed. A status field with values like “active” is typed string; narrow it to a union by hand if you want stricter types.
  • Names may need tidying. Generated names come from keys, so rename them to fit your project.

Frequently asked questions

Should I use interface or type?
For plain object shapes both work the same. Interfaces can be extended and merged, and many style guides prefer them for objects; type aliases can also describe unions and other forms. Choose with "Declare as" to match your codebase.
How does it decide which fields are optional?
When the JSON is an array of objects, the converter looks at every item, not just the first. A field that appears in some items but not others gets a question mark after its name, which makes it optional.
Why is a field typed as null?
Your sample only had null for that field, so the converter cannot know what it holds the rest of the time. Edit the result to something like string | null once you know the real type.
Does it validate data at runtime?
No. TypeScript types disappear when code is compiled, so they describe data but do not check it. To check incoming data, use a runtime validator or a JSON Schema; the JSON schema validator can test a sample.
Is my JSON sent anywhere?
No. The types are generated in your browser.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close