GoGood.dev
TS

JSON to TypeScript

Convert JSON objects to TypeScript interfaces or type aliases. Handles nested objects, arrays, and union types.

JSON Input

Lines: 0
Chars: 0
Size: 0 B

TypeScript Output

Lines: 0
Chars: 0
Size: 0 B

Understanding TypeScript Interfaces and Type Generation

TypeScript interfaces and type aliases define the shape of objects in your code, enabling the compiler to catch type errors before runtime. When working with APIs, databases, or configuration files that return JSON data, manually writing interfaces for every response is tedious and error-prone. This JSON to TypeScript tool automates that process by analyzing sample JSON and generating ready-to-use TypeScript interfaces. Instead of spending minutes typing out property names and inferring types, you paste a JSON sample and instantly get a fully typed interface that matches your data structure.

Type safety is one of TypeScript's greatest strengths. It allows IDEs to provide precise autocomplete suggestions, catches misspelled property names at compile time, and documents your code structure for other developers. Using this tool to generate types from JSON ensures your interfaces exactly match your data and reduces the risk of runtime errors caused by undefined properties or type mismatches.

Common Use Cases

  • API Response Typing: Generate TypeScript interfaces from API response samples to type-check your HTTP requests and data handling code.
  • GraphQL Query Results: Convert GraphQL query result samples to TypeScript interfaces for end-to-end type safety in your Apollo or Urql clients.
  • Configuration Files: Create type definitions from configuration file samples (package.json, tsconfig.json, app config) for strongly-typed config objects.
  • Database Records: Generate interfaces from database query results or sample documents to type your ORM operations and data models.
  • Rapid Prototyping: Quickly scaffold type definitions while building new features, then refine them as your data model evolves.

How to Use This Tool

  1. Paste JSON Sample: Copy and paste a JSON object or array into the input panel. A real-world sample works best because it shows the actual structure.
  2. Configure Options: Choose between interface or type alias output, select your naming convention (camelCase or PascalCase), and set indent size. Use Optional to add ? to properties, or Readonly to add the readonly modifier.
  3. Set Root Name: Enter a root interface name (default is "Root"). Nested objects are automatically named based on their property keys.
  4. Generate Types: The tool instantly generates TypeScript interfaces in the output panel, organized from innermost (bottom) to outermost (top) types.
  5. Copy & Use: Copy the output and paste it directly into your TypeScript files, or download as a .ts file for easy integration.

Features

Nested Object Generation

Deeply nested JSON automatically produces separate named interfaces ordered from innermost to outermost, with type references between them.

Array Type Inference

Arrays with a single element type generate typed arrays (Type[]). Mixed-type arrays generate union types (Type1 | Type2)[].

Property Modifiers

Toggle Optional to add ? modifiers, or Readonly to add readonly modifiers to every property for stricter type control.

Null Handling

Null values in JSON are correctly typed as null literals or optional properties, preserving null information in your type definitions.

Frequently Asked Questions

What is the difference between interface and type in TypeScript?

Interfaces are primarily used for object shapes and support declaration merging, making them ideal for APIs and public contracts. Types are more flexible and support unions, intersections, and primitives. For most object definitions, interfaces are preferred, but this tool lets you choose. Use interfaces for API contracts and types for utility definitions.

How does the tool handle null values?

Null values in JSON are inferred as optional properties (using ?) or as explicit null literal types, depending on context. If a property is sometimes null and sometimes a value, it's typed as ValueType | null to preserve the null possibility.

What happens with deeply nested JSON?

Deeply nested JSON generates multiple interfaces, with innermost types defined first (bottom-up ordering). Each nested object becomes its own interface with a descriptive name based on the property key, making types composable and reusable.

Can I generate types for JSON arrays?

Yes! Paste a JSON array and the tool generates a type for the array element. If elements have consistent structure, it generates a single interface. If elements are mixed types, it generates a union type representing all possibilities.