How to use the JSON to TypeScript generator
- Paste a JSON object, or an array of objects.
- Name the root type and choose interface or type.
- Press Convert, then copy or download the .ts file.
Everything runs in your browser. Nothing you type is sent to our servers. Your last input is remembered in this browser only.
How the types are worked out
Every object in an array is merged, so a key that only some objects have becomes optional (name?: string), and a value that is null in some objects is typed as string | null. An array with mixed values becomes (string | number)[]. Nested objects become their own interfaces, named after the key, and array item types are singular: orders becomes Order[].
Keys that are not valid identifiers, such as "first-name", are quoted. Values that are only ever null are typed null, and empty arrays are unknown[], because the sample does not say what they hold.
export interface RootObject {
id: number;
tags?: string[];
note?: null;
}
/** The JSON root is an array: */
export type RootObjectList = RootObject[];Types do not check data at runtime
TypeScript types disappear when the code is compiled, so an interface cannot stop a bad API response from reaching your code. For data you do not control, validate it with a schema library such as Zod or Valibot, and derive the type from the schema. Use the generated interface as the starting point for that schema.
When you would use it
- Typing a fetch call to a third-party API from a sample response.
- Writing types for a JSON config or fixture file.
- Sharing a contract between a back end and a front end team.
Frequently asked questions
Interface or type?
For object shapes they are nearly interchangeable. Interfaces can be extended and merged; type aliases can also express unions. Many teams pick one for consistency. This tool can produce either.
What does "Make every field optional" do?
It adds ? to every property. That suits partial updates, or APIs that leave fields out freely, but it means you must check every field before using it.
Why is a date typed as string?
JSON has no date type, and JSON.parse returns ISO dates as strings. Convert them with new Date() where you need a Date object.
Why are numbers just number?
TypeScript has one number type for integers and decimals. Very large integer IDs lose precision in JavaScript, so if your API sends them, ask for them as strings.