JSON to TypeScript Converter
Infer an editable TypeScript type from a JSON example, including nested objects and mixed arrays.
Runs in your browser. Your input stays on this device.
What this tool does
Infer a draft type alias for one JSON sample, including nested properties and inferred array element types.
When to use it
Start modeling a new API without typing every observed field by hand. Review optional fields and other response variants before using the type.
How to use it
- Paste a representative JSON body without secrets.
- Select Generate TypeScript and copy the Root type.
- Rename it and refine nullability, optional keys and unions against your contract.
Worked example
Convert {"id":1,"label":"demo","tags":["api","test"],"metadata":null}. The output describes id as a number, label as a string, tags as an array of strings and metadata as null. Copy the Root type into a TypeScript file and rename it to match your domain, such as ItemResponse. If production examples show metadata as an object as well, update that property to describe both possibilities. Compare several representative payloads before committing a type generated from just one response.
{"id":1,"tags":["api"]}export type Root = {
"id": number;
"tags": Array<string>;
};Supported features and limitations
The type reflects the example alone. Review optional properties and empty arrays, which become unknown[]. The generated type provides compile-time checks.
Common mistakes and edge cases
An empty array has no elements to infer from and becomes unknown[]. A missing property in the input is absent from the output, while a property explicitly set to null gets the null type. Mark keys optional when API documentation says they may be omitted. Review mixed arrays and decide whether the union describes real variation or an upstream data issue. Preserve property spelling if the TypeScript type represents the network format. If your API represents several variants with a shared discriminator, generate a draft for each variant separately. Then use the discriminator to describe the union explicitly so code can narrow the type when it reads that field.
Frequently asked questions
- Does a generated type validate incoming data at runtime?
- TypeScript types help the compiler check your source. Data that arrives over the network still needs runtime validation when correctness depends on its shape. Use the API contract and a runtime parser where appropriate.
- Why are property names quoted?
- Quoted property names also cover valid JSON keys such as user-id or display name, which cannot be written as ordinary TypeScript identifiers. The resulting type preserves those names so property access matches the source data.