Paste your JSON below to generate TypeScript interfaces:
Loading...
Why Generate TypeScript from JSON?
TypeScript interfaces provide type safety for your JSON data. Instead of manually writing interfaces, generate them automatically from API responses or sample JSON data.
Example Conversion
Input JSON:
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"isActive": true
}Generated TypeScript:
interface User {
id: number;
name: string;
email: string;
isActive: boolean;
}Features
- Nested object support โ Creates separate interfaces for nested objects
- Array type inference โ Detects array item types automatically
- Custom root name โ Name your root interface anything you want
- Special character handling โ Properly quotes keys with special characters
Type Inference Rules
| JSON Value | TypeScript Type |
|---|---|
"string" | string |
123, 45.67 | number |
true, false | boolean |
null | null |
[1, 2, 3] | number[] |
["a", 1] | (string | number)[] |
{ ... } | Separate interface |
Handling Nested Objects
Nested objects automatically get their own interfaces with descriptive names:
// Input
{
"user": {
"profile": {
"name": "John"
}
}
}
// Output
interface RootUserProfile {
name: string;
}
interface RootUser {
profile: RootUserProfile;
}
interface Root {
user: RootUser;
}Working with API Responses
Common workflow for typing API data:
- Make an API request and copy the JSON response
- Paste it into this tool
- Name the root interface (e.g.,
ApiResponse) - Copy the generated interfaces to your TypeScript project
- Use with
fetchor your HTTP client
// Usage in TypeScript
const response = await fetch('/api/users');
const data: User[] = await response.json();
// Now you have full type safety
data.forEach(user => {
console.log(user.name); // TypeScript knows this is a string
});Pro Tips
- ๐ก Use representative data โ Include all possible fields in your sample JSON for complete interfaces
- ๐ก Handle optional fields โ If a field might be missing, manually add
?after generation - ๐ก Union types โ For fields that can be multiple types, consider using union types manually
- ๐ก Validate first โ Use the JSON Validator to ensure your JSON is valid before converting
Limitations
- Cannot detect optional properties (all properties are required)
- Cannot infer union types for non-array values
- Date strings are typed as
string, notDate
For these cases, manually adjust the generated interfaces after copying.
Related Tools
- JSON Validator โ Validate your JSON first
- JSON Schema Validator โ Another way to define JSON structure
- JSON to CSV โ Export JSON data