How to use the JSON to TypeScript converter
- Paste a JSON sample, such as an API response, or select Load example.
- Set the root type name and choose your style options. The TypeScript updates as you type.
- Copy the types or download them as a
.tsfile.
How types are chosen
Types come from every value in your sample, not just the first one, so a richer sample gives more precise types:
- Arrays of objects are merged into one type. A property that's missing from some items becomes optional (
discount?: number), and one that's sometimesnullbecomesstring | null. - Arrays that mix types keep every type as a union, such as
(number | string)[]. - Objects keyed by IDs, such as
{"101": {...}, "102": {...}}, becomeRecord<string, T>. - Identical shapes share one type, and type names come from property names, made singular for array items (
items→Item). - Property names stay exactly as in the JSON, quoted where needed (
'first-name'), so the types match whatJSON.parsereturns.
Values that are only ever null, and empty arrays, become unknown (or any, if you choose). The converter tells you when this happens, so you can add a better sample.
Dates and large numbers
JSON.parse returns dates as strings, so date values are typed as string. Convert them with new Date(value) where you need them. Integers larger than Number.MAX_SAFE_INTEGER (9,007,199,254,740,991) lose precision when parsed; the converter warns you when your sample has one, because such IDs are best sent as strings.
Using the generated types
const response = await fetch('/api/orders/1042');
const order = (await response.json()) as Root;
console.log(order.customer.email);
A type assertion like this trusts the server. If the data comes from outside your control, validate it at runtime, for example with a schema library, before relying on the types.
Frequently asked questions
Is my JSON uploaded?
No. The conversion runs entirely in your browser; nothing is sent to our servers or logged.
Should I use interface or type?
Either works for JSON data. Interfaces are the common choice for object shapes and give clearer error messages; type aliases suit codebases that use them everywhere. The structure generated is the same.
Is the generated code tested?
Yes. Our test suite type-checks the converter's output with the TypeScript compiler for a range of samples and options. Each check also confirms the original JSON is accepted by the generated types, and that values of the wrong type are rejected.
Can I convert a JSON array?
Yes. For an array of objects, the item type takes the root name and a list type is added, such as type RootList = Root[].