JSON to TypeScript Generator
Paste a JSON payload and get TypeScript interfaces or type aliases with nested types, unions, optional fields and quoted keys inferred from the sample.
The interactive tool loads here when JavaScript runs.
About this toolWhat is the JSON to TypeScript Generator?
The JSON to TypeScript Generator reads a sample JSON payload, works out the shape of every object in it, and writes TypeScript declarations you can paste into your code. Nested objects become their own named interfaces, arrays get element types, and fields that appear in only some array items become optional.
Options control the output style: interfaces or type aliases, export on every declaration, readonly properties, the root type name, and whether a field whose value is null should be marked optional. Generate, then copy the result or download it as Root.ts (or whatever you named the root).
The types are inferred from one example, so they can only be as complete as that example. A field that happens to be null in your sample is typed as null, and a field that is missing from every item is not there at all. Use a payload with a few varied records when you can.
- Nested interfaces named from their keys, with singular names for array items
- Optional fields inferred across array items
- Union types for mixed arrays, written with correct parentheses
- Interfaces or type aliases, export, readonly and null options
- Runs in your browser; payloads are not uploaded
Updated
How to use the JSON to TypeScript Generator
- Paste a JSON payload into JSON Input, or click Load Sample to see the format.
- Set Root type name, for example ApiResponse. It is converted to PascalCase.
- Choose options: Use interfaces (vs. type aliases), Export all, Treat nulls as optional (?), and Readonly fields.
- Click Generate TypeScript. If the JSON is invalid, the parser's error message appears under the input.
- Copy the TypeScript Output or download it as a .ts file named after the root type.
Worked examples
Nested array with an optional field
Input
{"id":7,"user-name":"ada","email":null,"tags":["a","b"],"orders":[{"sku":"A1","qty":2},{"sku":"B2","qty":1,"note":"gift"}]}Output
export interface Root {
id: number;
"user-name": string;
email?: null;
tags: string[];
orders: Order[];
}
export interface Order {
sku: string;
qty: number;
note?: string;
}orders is singularised to an Order interface, and note is optional because only one of the two orders has it. user-name is quoted because it is not a valid identifier. email is null in the sample, so the best the generator can say is null, marked optional by the default option.
A top-level array with nulls kept
Input
[{"id":1,"name":"a"},{"id":2,"name":null}] (Treat nulls as optional switched off)Output
export type Root = RootItem[];
export interface RootItem {
id: number;
name: string | null;
}A root array becomes a type alias pointing at an item interface. With the null option off, the field keeps the honest string | null type, which is what most strict code should use.
Background and insight
TypeScript's type system is structural: an object satisfies an interface if it has the right properties with the right types, whatever it was called when it was created. That makes generating types from JSON practical. The generator only needs to describe the shape, not a class hierarchy, and the same interface can describe data from fetch(), a database driver or a test fixture.
Arrays are where the inference matters. When an array holds objects with different keys, the generator merges them into one interface and marks the keys that are missing from some items as optional, which matches what you actually need to check before reading them. When it holds a mix of kinds, such as numbers and strings, it produces a union element type written as (number | string)[]. The parentheses matter: number | string[] means "a number, or an array of strings".
Generated types are compile-time only. They tell the editor and the compiler what you expect, but nothing checks at run time that the server really sent that shape. For data that crosses a trust boundary, pair the types with a runtime validator such as Zod, or generate both from an OpenAPI or JSON Schema contract.
Common use cases
Typing a third-party API
Paste a real response from an API with no published types and get a starting point for a typed client.
Typing JSON config files
Generate an interface for a settings.json or feature-flag file so typos in property names fail at compile time.
Fixtures and mocks
Type the fixtures in a test suite from the JSON files they are loaded from.
Migrating JavaScript to TypeScript
Replace any on data-heavy modules by generating interfaces from logged sample values.
Tips and limitations
- Feed it several records, not one. Optional fields and union types can only be inferred from variety in the sample.
- Empty arrays become any[] because there is nothing to infer from. Replace them by hand or add one sample element.
- JSON has one number type, so integers and floats are both number. Dates arrive as strings and are typed as string.
- Treat nulls as optional (?) is convenient but loose: email?: string accepts undefined, not null. Under strictNullChecks, switch it off and keep string | null when your API really sends null.
- Keys that are not valid identifiers, and reserved words like class, are quoted. Type names that would start with a digit get a T prefix, so a key named 2fa yields an interface T2fa.
Sources and further reading
- Type Compatibility, TypeScript Handbook, Microsoft. Why TypeScript's structural typing lets one interface describe data from any source
- Everyday Types, TypeScript Handbook, Microsoft. Union types, optional properties, interfaces and type aliases
- RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format, IETF. The JSON value types the generator maps from
- Zod documentation, Zod. A runtime validator that infers TypeScript types from a schema
Frequently asked questions
How do I convert JSON to a TypeScript interface?
Paste the JSON into the input, set a root type name, and click Generate TypeScript. Each object in the payload becomes an interface, arrays get element types, and nested objects are named after their keys. Copy the output into a .ts file and import it where you parse the data.
Should I use interface or type for JSON data?
For plain object shapes, either works and they behave almost identically. Interfaces can be extended and merged by redeclaration, while type aliases can also express unions and mapped types. Pick one convention per codebase; the switch in Options changes every declaration at once.
Why is a field typed as null in the generated interface?
Because that field is null in your sample, and null is all the generator can see. Add a record where the field has a real value and it will become a union such as string | null, or edit the type by hand.
How are keys with dashes or spaces handled?
They are written as quoted property names, such as "user-name": string, which is valid TypeScript. You then read them with bracket notation, data["user-name"]. Nested type names are built from the key in PascalCase, so user-profile becomes UserProfile.
Do generated TypeScript types validate JSON at run time?
No. TypeScript types are erased when compiled, so they cannot reject a bad response. Use them alongside a runtime validator such as Zod or Ajv for data from outside your program.
What happens with an array that mixes objects and numbers?
The element type becomes a union. Objects in the array are merged into one interface with optional fields, so [1, {"a": 1}, {"b": "x"}] gives (number | Item)[] where Item has a?: number and b?: string.
Is there an in-depth guide to the JSON to TypeScript Generator?
Yes. “From JSON to TypeScript Types: Inference, Optional Fields and Nulls” covers the background, how it works, common pitfalls and worked examples. Read the full guide.
Related Tools You May Like
Free, in-browser tools that pair well with this one.