JSON to TypeScript Converter
Convert JSON to TypeScript interfaces or type aliases instantly. Nested objects become named interfaces, arrays are merged into one type with optional fields, and mixed values become union types.
Generate TypeScript Types from JSON
Typing an API response by hand is slow and easy to get wrong, especially when objects are nested several levels deep. Paste a sample response into this JSON to TypeScript converter and it writes the interfaces for you, with a separate, named interface for every nested object.
It looks at every item of an array, not just the first one, so keys that only appear in some items become optional and values that change type become unions. The conversion runs entirely in your browser, so private payloads never leave your computer.
How to convert JSON to TypeScript
- Paste your JSON into the input box, for example a response copied from your browser's network tab or from an API client. Click Format JSON to pretty-print it.
- Name the root type, for example
UserorOrderResponse. Nested types are named after their keys. - Choose the options:
interfaceortype, how missing keys are written, and whether to addexportandreadonly. - Copy the TypeScript into a
types.tsfile. The output updates as you type.
How the types are inferred
The converter walks through the whole document and records which kinds of values it sees for every key. It then turns that into types:
- Nested objects become their own interface, named in PascalCase after the key:
billing_addressbecomesBillingAddress. Identical shapes are reused instead of duplicated. - Arrays of objects are merged into one item type. A key that is missing from some items is marked optional, so
[{"id": 1, "note": "x"}, {"id": 2}]givesid: number; note?: string;. Item types are named in the singular:projectsgivesProject[]. - Mixed arrays become union types:
[1, "two", true]gives(string | number | boolean)[]. - null is kept as
null. When a key is null in one item and a string in another, the type isstring | null. - Empty arrays and objects become
unknown[]andRecord<string, unknown>, because there is nothing to infer from. - Keys that are not valid identifiers, such as
postal-codeor2fa, are quoted:"postal-code": string;.
JSON to TypeScript type mapping
| JSON value | TypeScript type | Example |
|---|---|---|
| String | string | "Ada" |
| Number (integer or decimal) | number | 42, 98.5 |
| true / false | boolean | true |
| null | null (or T | null) | null |
| Object | Named interface | {"city": "London"} → Address |
| Array of one type | T[] | ["a", "b"] → string[] |
| Array of mixed types | (A | B)[] | [1, "a"] → (string | number)[] |
| Empty array | unknown[] | [] |
Interface or type alias?
Both describe the shape of an object and work the same for API data. interface declarations can be extended with extends and merged when declared twice, and editors show their names in error messages. A type alias can also describe unions and tuples, so some teams use type everywhere for consistency. Pick whatever your codebase already uses; the generated fields are identical.
For missing keys, the default name?: T matches APIs that leave keys out. Choose name: T | null if your API always sends the key but sets it to null.
Tips for better generated types
- Use a representative sample. Types can only reflect the data you paste. Include several array items, and records where optional fields are empty, so optional keys and nulls are detected.
- Turn strings into unions. A field such as
status: stringis often really"active" | "trial" | "cancelled". Narrow it by hand after generating. - Dates are strings. JSON has no date type, so
createdAtstays astring. Convert it to aDatewhen you parse the response. - Validate at runtime. TypeScript types disappear after compilation. For data from outside your app, check it with a schema validator before trusting the types.
- Writing a Go backend for the same API? Use the JSON to Go converter to get matching structs. For XML APIs, convert the response with XML to JSON first.
Frequently Asked Questions
Common questions about converting JSON to TypeScript.
Paste the JSON into the input box above. The converter writes one interface for the root object and one for every nested object, which you can copy into your project.
Yes. Every nested object becomes its own named interface, and arrays of objects are merged into a single item type, so all keys from all items are included.
When an array contains several objects, a key that is missing from at least one of them is marked optional with a question mark, or written as T | null if you choose that option.
A key that is always null gets the type null. If it is null in some items and has a value in others, you get a union such as string | null.
For describing JSON objects they are interchangeable. Interfaces can be extended and merged; type aliases can also express unions. Use the style your project already follows.
TypeScript has a single number type for integers and decimals. If you need to tell them apart, use the JSON to Go converter, which distinguishes int64 and float64.
No. The conversion runs entirely in your browser with JavaScript, so the data you paste is never sent to a server.
No, because JSON stores dates as strings. The generated type is string; convert it with new Date(value) when you parse the data.