← All guides

JSON to TypeScript

Quick Answer

Use the JSON Schema Generator to convert JSON into TypeScript interfaces. Paste your JSON, select the TypeScript tab, and copy the generated interfaces. The tool infers types automatically from your data structure.

Why Convert JSON to TypeScript?

  • Type Safety: Catch errors at compile time instead of runtime
  • IDE Support: Get autocomplete and IntelliSense for API responses
  • Documentation: Interfaces serve as living documentation
  • Refactoring: Easily update types when APIs change

Step-by-Step Instructions

  1. Open the tool: Navigate to the JSON Schema Generator.
  2. Select TypeScript tab: Click on the "TypeScript" tab to ensure TypeScript output.
  3. Paste your JSON: Copy your JSON sample and paste it into the input area on the left.
  4. Review the output: The tool generates TypeScript interfaces on the right side.
  5. Copy to your project: Click the copy button or select and copy the generated code.
  6. Paste into your TypeScript file: Add the interfaces to your project.

Example

Input JSON:

{
  "id": 123,
  "name": "John Doe",
  "email": "john@example.com",
  "isActive": true,
  "roles": ["admin", "editor"],
  "profile": {
    "avatar": "https://example.com/avatar.jpg",
    "bio": "Software developer"
  }
}

Generated TypeScript:

interface User {
  id: number;
  name: string;
  email: string;
  isActive: boolean;
  roles: string[];
  profile: Profile;
}

interface Profile {
  avatar: string;
  bio: string;
}

Type Inference Rules

The tool makes intelligent decisions about types:

  • Numbers: All numeric values become number
  • Strings: Text values become string
  • Booleans: true/false become boolean
  • Arrays: Become Type[] with the appropriate element type
  • Objects: Become nested interfaces
  • Null values: Become union types like string | null

Best Practices

  • Use representative data: Paste JSON that includes all possible fields and value types
  • Handle nulls: If a field can be null, include null in your sample JSON
  • Review generated types: The tool infers from data, so verify types match your API contract
  • Use strict mode: Enable TypeScript strict mode for maximum type safety

Common Problems and Solutions

Problem: All array items typed as any

Solution: Ensure your sample JSON has consistent types in arrays. Mixed types will result in any[] or union types.

Problem: Optional fields marked as required

Solution: The tool can't infer optionality from a single sample. Manually add ? to optional fields: name?: string

Problem: Date strings typed as string instead of Date

Solution: The tool treats dates as strings. Manually change to Date if your application parses them.

Related Tools

FAQ

Q: Can I generate types from multiple JSON samples?
A: The tool processes one sample at a time. For complex APIs, consider combining samples or using tools like quicktype that support multiple samples.

Q: Does it support JSON Schema?
A: Yes, the tool also generates JSON Schema which can be used with various validators and code generators.

Q: Can I customize interface names?
A: The tool generates names based on keys. You'll need to manually rename interfaces after copying to your project.

Related tools