JSON to TypeScript
Paste a JSON sample and the tool infers TypeScript interfaces that match its shape. Fields are typed by observed values (string, number, boolean, Array<T>), nested objects get their own named interfaces, and fields observed as null or missing become optional (?) or nullable (| null) depending on the style you prefer.
How to convert JSON to TypeScript
-
1
Paste JSON
A single sample is enough; multiple samples improve nullability and union inference.
-
2
Pick output style
`interface` (default), `type` alias, or readonly interface with all fields marked `readonly`.
-
3
Pick optional strategy
Mark fields `?` (may be absent) or `| null` (always present, may be null).
-
4
Copy the types
Drop into a `.ts` file and you have strongly typed access to the API response.
Example
Input:
{ "id": 1, "name": "Alice", "age": null, "tags": ["admin", "user"], "address": { "city": "Madrid" } }
Output:
interface User {
id: number;
name: string;
age: number | null;
tags: string[];
address: Address;
}
interface Address {
city: string;
}
Type mapping
| JSON | TypeScript |
|---|---|
| string | string |
| integer / decimal | number |
| boolean | boolean |
| null alone | null |
| null + T | T | null (or T?) |
| array of T | T[] |
| mixed array | (T1 | T2)[] |
| object | Named nested interface |
| empty array | unknown[] (cannot infer) |
Optional vs nullable
foo?: string, the field may be absent from the object.undefined-checking applies.foo: string | null, the field is always present but may explicitly be null.foo?: string | null, could be absent OR null.
JSON itself does not have undefined, but APIs vary on how they signal absence. Match your API semantics:
- REST APIs typically omit missing fields ->
?:. - GraphQL always returns every requested field ->
| null. - Some SDKs use both in different contexts.
Union vs literal types
If the tool sees the same string field with a small set of values across samples ("status": "pending", "active", "archived"), it can emit a string literal union:
status: "pending" | "active" | "archived";
Toggle “infer string literal unions” if you want this.
Common mistakes
- Inferring from one sample. Every field becomes required; nullability cannot be observed. Pass 5-10 varied samples for better types.
- Empty arrays.
"tags": []gives no type info, the generator emitsunknown[]. Provide a sample with at least one element. - Mixed-type arrays.
[1, "two", true]produces(number | string | boolean)[]. Usually this means the JSON should be redesigned rather than typed. - Numeric string keys. JSON
{"1": "a", "2": "b"}is still an object in TypeScript (Record<string, string>), not an array. The generator handles this correctly.
Frequently Asked Questions
Match your API. REST APIs that drop null fields want ?:. GraphQL, which always returns every selected field, wants | null. When in doubt, T | null with required syntax is stricter and catches more bugs at compile time.
Yes, if you enable it and provide multiple samples. A field observed with 2-5 distinct string values across samples is emitted as a literal union. Beyond that threshold it falls back to string.
interface for most cases, it’s open to extension and TypeScript optimizes it better. type aliases are useful for unions, intersections, tuples, and mapped types. For JSON-derived types, either works; pick a project convention.
Yes. Each nested object becomes its own interface, with names derived from the key (user.address -> Address). For very deep or repetitive structures, consider a JSON Schema and a dedicated schema-to-TS generator.
Related Tools
ASCII Table Reference
Full ASCII table from 0 to 127 with decimal, hex, octal, binary, standard names and HTML numeric-reference notation, including NUL, LF and DEL.
Color Palette Generator
Generate monochromatic, analogous, complementary, triadic or tetradic color palettes from a base HEX color and export copy-ready CSS variables.
HTML Character Reference
Searchable list of HTML entities, their named and numeric codes, and a one-click copy for special characters and symbols.
FPS Counter
Measure browser FPS with requestAnimationFrame, smoothing, min/max frame rate, warnings and an optional graph. Runs locally with no upload or API.
HEX Color Picker
Pick or enter a HEX colour and get RGB, HSL, approximate CMYK, relative luminance and contrast ratios against white and black.
JSON Formatter
Paste JSON to pretty-print with 2 or 4 spaces, minify it to compact output, or run a quick syntax check before copying the result.
Tool available in other languages
- JSON เป็น TypeScript [TH]
- JSON a TypeScript [ES]
- JSON vers TypeScript [FR]
- JSON para TypeScript [PT]
- تحويل JSON إلى TypeScript [AR]
- JSON zu TypeScript [DE]
- JSON naar TypeScript [NL]
- JSON ke TypeScript [ID]
- JSON에서 TypeScript로 [KO]
- JSON sang TypeScript [VI]
- JSON till TypeScript [SV]
- JSON do TypeScript [PL]
- JSONからTypeScriptへ [JA]
- JSON a TypeScript [IT]
- JSON в TypeScript [RU]
- JSON'dan TypeScript'e [TR]
- JSON 转 TypeScript [ZH]