EthicalFusion

JSON to TypeScript Interface

Generate TypeScript interfaces from a JSON sample — with nested types.

Was this tool helpful?

Thanks for the feedback!

Paste a sample JSON object, or an array of objects, and this generator writes matching TypeScript interfaces, creating a separate interface for every nested object. It is meant for front-end and Node.js developers who want typed API responses or config objects without writing each property by hand.

Example

With the Root interface name left as Root, this input:

{"id": 1, "user": {"name": "Asef", "tags": ["a", "b"]}}

produces:

export interface Root {
  id: number;
  user: User;
}

export interface User {
  name: string;
  tags: string[];
}

The root interface comes first and nested interfaces follow. Every interface is exported, so you can paste the block into a types.ts file and import from it.

How types are inferred

JSON valueTypeScript type
"text"string
42 or 3.14number
true / falseboolean
nullany
[] (empty array)any[]
["a", "b"]string[] (from the first element)
[[1, 2], [3]]number[][]
{ ... }a new interface named after the key

Interface names come from the property name with its first letter capitalised and symbols removed, so user becomes User and items becomes Items. Property names that are not valid identifiers are quoted, for example "order-id": string;. The Root interface name field is cleaned the same way, so "Order Response" becomes OrderResponse. If you paste a top-level array, the root interface describes one element, and you would type the whole response as Root[].

What to review after generating

The generator sees one sample, not your API's documentation, so a quick review makes the types trustworthy:

  • Replace any. A null in the sample becomes any, which switches off type checking. If the field is a nullable string, change it to string | null.
  • Mark optional fields. Every property is required. If a field is sometimes missing, add ?, as in coupon?: string;.
  • Check arrays of objects. Only the first element is inspected, so fields that appear only in later elements are missed.
  • Rename plural interfaces. An items array produces an interface called Items; OrderItem is usually clearer.
  • Watch repeated key names. If two different nested objects share a key, such as meta under both billing and shipping, only the first shape is used for both.
  • Narrow strings. A status field arrives as string; you may want a union like "paid" | "pending".

Using the interfaces

Annotate the parsed response, for example const data: Root = await res.json();. Keep in mind that TypeScript types disappear at runtime: they describe what you expect, but do not check what the server actually sends. For untrusted input, add runtime validation as well.

If the JSON will not parse, the error message names the problem; tidy it first in the JSON Formatter. Spreadsheet data can be turned into JSON with the Excel to JSON Converter and then typed here. Your sample stays in the browser, which matters when it contains real customer records.

Frequently asked questions

How are arrays typed?
By their first element, so ["a","b"] becomes string[]. An empty array becomes any[], and nested arrays such as [[1,2]] become number[][].
Why is a field typed as any?
Its value in the sample was null, so the real type could not be inferred. Replace any with the correct type, such as string | null.
Does the generator mark optional properties?
No. All properties are required. Add a question mark to any field that the API sometimes leaves out.
Can I paste an array of objects?
Yes. The first object in the array is used to build the root interface, and the whole array is then typed as Root[] in your code.
Does it create type aliases or classes?
No. The output is always exported interfaces, one for the root and one for each nested object.

Related tools

Embed this tool on your site

Paste it into any blog or website. Free, with a credit link.