How to Convert JSON to TypeScript Interfaces
Step-by-step guide to converting JSON data to TypeScript interfaces. Learn best practices, common patterns, and tools for generating type definitions.
By JSON Viewer Team
Published on January 5, 2026
Introduction
TypeScript interfaces provide type safety and better developer experience when working with JSON data. Converting JSON to TypeScript interfaces helps catch errors at compile time and improves code quality. This guide will show you how to do it effectively.
Why Convert JSON to TypeScript?
Converting JSON to TypeScript interfaces offers several benefits:
- Type Safety: Catch errors at compile time
- IntelliSense: Better autocomplete in your IDE
- Documentation: Interfaces serve as inline documentation
- Refactoring: Easier to refactor with type checking
- Team Collaboration: Clear contracts for data structures
Manual Conversion Method
Let's start with a simple JSON example and convert it manually:
Step 1: Analyze Your JSON Structure
Consider this JSON data:
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"age": 30,
"isActive": true,
"address": {
"street": "123 Main St",
"city": "New York",
"zipCode": "10001"
},
"hobbies": ["reading", "coding"]
}
Step 2: Create the Interface
Convert each property to TypeScript:
interface Address {
street: string;
city: string;
zipCode: string;
}
interface User {
id: number;
name: string;
email: string;
age: number;
isActive: boolean;
address: Address;
hobbies: string[];
}
Type Mapping Rules
Here's how JSON types map to TypeScript:
| JSON Type | TypeScript Type |
|---|---|
| String | string |
| Number | number |
| Boolean | boolean |
| Null | null | undefined |
| Object | interface or type |
| Array | Type[] or Array<Type> |
Handling Optional Properties
If a property might not exist, use the optional modifier (?):
interface User {
id: number;
name: string;
email?: string; // Optional property
age?: number; // Optional property
}
Handling Nullable Values
For properties that can be null, use union types:
interface User {
id: number;
name: string;
email: string | null; // Can be string or null
}
Handling Arrays
Arrays in JSON map to TypeScript array types:
// Simple array
hobbies: string[]
// Array of objects
users: User[]
// Mixed array (use union types)
items: (string | number)[]
Handling Nested Objects
Create separate interfaces for nested objects:
interface Address {
street: string;
city: string;
zipCode: string;
}
interface User {
name: string;
address: Address; // Reference the nested interface
}
Using Type Aliases
For simple types or unions, you can use type aliases:
type Status = "active" | "inactive" | "pending";
interface User {
id: number;
status: Status;
}
Common Patterns
API Response Pattern
interface ApiResponse<T> {
success: boolean;
data: T;
message?: string;
errors?: string[];
}
interface User {
id: number;
name: string;
}
// Usage
const response: ApiResponse<User> = {
success: true,
data: { id: 1, name: "John" }
};
Pagination Pattern
interface PaginatedResponse<T> {
items: T[];
total: number;
page: number;
pageSize: number;
hasMore: boolean;
}
Best Practices
- Use Descriptive Names: Interface names should clearly describe the data structure
- Keep Interfaces Focused: Don't create overly complex interfaces
- Reuse Common Types: Extract common patterns into reusable types
- Document Complex Types: Add comments for non-obvious properties
- Use Generics: For reusable patterns like API responses
- Version Your Interfaces: When APIs change, version your interfaces
Automated Conversion Tools
While manual conversion gives you full control, automated tools can speed up the process:
- JSON to TypeScript Converters: Online tools that generate interfaces
- Quicktype: Command-line tool for generating types
- json2ts: Online converter for JSON to TypeScript
Our JSON Viewer includes a built-in JSON to TypeScript converter that generates interfaces automatically!
Example: Complete Conversion
Let's convert a complex JSON structure:
JSON Input
{
"users": [
{
"id": 1,
"name": "John",
"profile": {
"avatar": "https://example.com/avatar.jpg",
"bio": "Developer"
}
}
],
"meta": {
"total": 100,
"page": 1
}
}
TypeScript Interfaces
interface Profile {
avatar: string;
bio: string;
}
interface User {
id: number;
name: string;
profile: Profile;
}
interface Meta {
total: number;
page: number;
}
interface UsersResponse {
users: User[];
meta: Meta;
}
Tips for Complex JSON
- Break down complex nested structures into smaller interfaces
- Use union types for properties that can have multiple types
- Consider using enums for fixed sets of values
- Use readonly for immutable data structures
- Add JSDoc comments for better documentation
Conclusion
Converting JSON to TypeScript interfaces is a valuable skill that improves code quality and developer experience. Whether you do it manually or use automated tools, having proper type definitions makes your codebase more maintainable and less error-prone.
Need to convert JSON to TypeScript? Try our free JSON Viewer - it automatically generates TypeScript interfaces from your JSON data!
Tags
Related Articles
Exporting JSON Data to Excel and CSV: A Complete Guide
Learn how to convert JSON data to Excel and CSV formats. Discover conversion methods, data transformation techniques, and practical use cases.
10 JSON Formatting Tips Every Developer Should Know
Discover 10 essential JSON formatting tips and tricks. Learn common pitfalls, pro techniques, and tools to improve your JSON workflow.
Using JSON in API Development: Best Practices
Learn best practices for using JSON in API development. Discover API design patterns, error handling strategies, and response formatting guidelines.