How to use the JSON to TypeScript tool
- Enter a name for the type under Type name, such as Tool or ApiResponse. It must be a valid identifier.
- Paste one representative JSON value into the JSON box.
- Select Generate type, or select Example to see the tool run on a sample object.
- Review the declaration in the Result box and select Copy to paste it into a .ts file.
How JSON to TypeScript inference works here
The tool parses the sample, walks it recursively and maps each JSON value to the TypeScript type that describes it. Strings become string, numbers become number, true and false become boolean, and null stays null. An object becomes an object type with one property per key. An array becomes an Array type whose element type is the union of the distinct shapes found inside it.
The output is a single export type alias. Nested objects are written inline, not split into separately named types. Keys that are valid identifiers are printed bare; any other key, such as one with a hyphen or a space, is quoted.
A worked example with nesting and null
This sample mixes scalars, a null, a nested object with a hyphenated key and an array of strings. The type name entered was Order.
Look at coupon: null. The sample carried no other value for that field, so the tool cannot know it is really a string that is sometimes null. Widen such fields by hand.
{"id":1042,"paid":false,"coupon":null,"ship-to":{"zip":"94107"},"tags":["gift","rush"]}
export type Order = {
id: number;
paid: boolean;
coupon: null;
"ship-to": {
zip: string;
};
tags: Array<string>;
};What the generator does not infer
A type inferred from one example describes that example, not the API contract. Samples nested more than about 40 levels deep are rejected, and the type name may contain only ASCII letters, digits, underscores and dollar signs, without a leading digit. Check these points before you commit the result.
- No optional properties. A key missing from your sample is missing from the type, and no property gets a question mark.
- Objects in an array are not merged. Two elements with different keys produce a union of two object types.
- An empty array becomes an Array of unknown, because there is no element to inspect.
- All numbers are number. Integers above 2^53 lose precision in JavaScript itself, so model those as string or bigint manually.
- No enums, literal types, Date detection or readonly modifiers.
- Keys that look like whole numbers are listed first in ascending order, since that is how JavaScript orders object keys.
Using the generated type in a project
Paste the alias into a types file and import it where you call the API. JSON.parse and response.json() return untyped data, so the alias gives compile-time help only. It does not check anything at runtime; pair it with a validation step when the input is not trusted.
For wider coverage, build the sample from several real responses: include every field at least once, put a non-null value wherever one can occur, and give each array at least one element.
Frequently asked questions
Does it generate an interface or a type alias?
A type alias, written as export type Name = { ... }. If your style guide prefers interfaces, change the keyword by hand and remove the equals sign.
How are arrays with mixed values typed?
Each element is typed on its own and duplicates are removed. An array holding 1, "a" and null becomes an Array of number | string | null, and an array of arrays is handled recursively by the same rule.
Why is a field typed as null?
The sample had null in that position and nothing else to learn from. Replace it with the real type, for example string | null, or generate again from a sample where the field is filled in.
Can the root value be an array or a plain string?
Yes. Any valid JSON value works. A top-level array of numbers produces an alias for an Array of number, and a bare string produces an alias for string. Invalid JSON returns the parser's error message and no type.
Is my JSON sample uploaded?
No. The sample is parsed and converted by script in this page, and no request carries it anywhere. The generated declaration contains only key names and types, never the values from your sample.