Skip to content

JSON to TypeScript Converter

Paste any JSON and get clean TypeScript interfaces — nested, deduplicated, and configurable.

Last updated:

Why Generate TypeScript Interfaces from JSON

Most web applications consume JSON from REST APIs, configuration files, or third-party services. Without explicit types, every property access is a guess — a renamed field or an unexpected null slips through unnoticed until it breaks at runtime. Converting a representative JSON response into TypeScript interfaces gives you a contract that the compiler can enforce across your entire codebase.

The benefits compound quickly. Your editor gains full autocomplete for every nested property, so you spend less time cross-referencing API docs. Refactors become safer because renaming a field surfaces every call site at build time rather than in production logs. And when onboarding new team members, a well-typed response object is documentation that never drifts out of date.

This tool automates the tedious part: paste a JSON payload, get clean, ready-to-use TypeScript interfaces in seconds. No manual transcription, no guessing at types.

Interface vs Type Alias — When to Use Each

TypeScript offers two main ways to describe the shape of an object: interface and type. Interfaces are open — they can be extended with extends or merged via declaration merging, which makes them a natural fit for API contracts that might evolve over time. Type aliases are closed and more flexible: they support unions, intersections, mapped types, and conditional types that interfaces cannot express directly.

For JSON-to-TypeScript conversion, interfaces are generally the better default. A JSON object maps cleanly to an interface with named properties, and the resulting code reads like a straightforward contract: interface User { id: number; name: string; }. That is why this tool generates interfaces by default. If your project style guide favours type aliases, converting is straightforward — replace interface X { with type X = { — but in most codebases the interface form is idiomatic for data shapes.

Where type aliases shine is when you need to express something an interface cannot: a discriminated union of API responses, a Pick or Omit utility type, or a union of string literals for an enum-like constraint. Use the generated interfaces as your starting point, then refine with type aliases where the domain demands it.

Handling Edge Cases

Real-world JSON is rarely as tidy as a textbook example. This tool accounts for the most common edge cases you will encounter when typing API responses:

  • Nullable fields — A value of null in the JSON sample is typed as unknown (or null when the context is clear), since the converter cannot infer the non-null type from a single sample. Review these fields and narrow them manually — for example, avatar: string | null.
  • Arrays of mixed types — When an array contains elements of different types (strings and numbers, or objects with different shapes), the tool produces a union type for the array items, such as (string | number)[]. For heterogeneous object arrays, it merges the shapes into a single interface with optional properties.
  • Nested objects — Deeply nested JSON structures generate separate named interfaces for each level rather than inlining anonymous types. This keeps the output readable and lets you reuse a nested interface (like Address) independently.
  • Optional vs required — From a single JSON sample every present key appears required. If your API sometimes omits a field, mark it optional (middleName?: string) after generation. A good practice is to paste the most complete response variant so the tool captures every possible field.
  • Empty arrays and objects — An empty array [] is typed as unknown[] and an empty object {} becomes an empty interface. Provide a populated sample when possible to get accurate types.

Think of the generated output as a strong first draft. It handles the mechanical work — counting nesting levels, choosing array element types, naming interfaces — so you can focus on the domain-specific refinements that only a human familiar with the API can make.

Related Tools

Before converting, make sure your JSON is valid. The JSON Formatter & Validator will catch syntax errors and pretty-print the payload so you can verify the structure at a glance. For exploring large or deeply nested responses interactively, the JSON Viewer provides a collapsible tree view that makes it easy to identify which objects deserve their own interface.

If you need a formal schema rather than TypeScript types — for example, to validate incoming webhooks at runtime — the JSON to JSON Schema converter generates a JSON Schema document from the same input. Together, these tools cover the full spectrum from ad-hoc exploration to compile-time safety to runtime validation.

Frequently Asked Questions

Can this handle nested objects and arrays?
Yes. Nested objects become their own named interfaces (with keys derived from the parent field name), and arrays are typed as `T[]` with `T` inferred from the array's elements. Arrays of mixed types become unions like `(string | number)[]`.
What happens with identical object shapes?
The tool computes a signature for each object shape (sorted keys plus field types) and deduplicates identical shapes into a single interface. If you have many objects with the same fields, you'll get one interface instead of dozens of copies.
Why not use quicktype?
Quicktype is excellent but ships ~500KB of JavaScript, which would bloat this site's bundle. This tool uses a focused ~150-line custom inference that covers 95% of real-world JSON and loads instantly. For edge cases (discriminated unions, complex enums), you may still want quicktype's CLI.
Does it generate interfaces or type aliases?
Interfaces by default — they are the most common choice in TypeScript codebases. Type aliases are emitted for unions and primitives where interfaces are not applicable.
How does it handle optional fields?
If any object in an array has a missing key, that key is marked optional (`?`) in the generated interface. This matches how TypeScript treats JSON data loaded from varied sources.
Can I paste a real API response?
Yes. Paste any JSON response body and the tool infers the full shape. For best results, include a representative sample with all the optional fields populated so the inference covers them.

Related Tools