ToolHub

JSON to TypeScript

Generate TypeScript types from JSON

JSON to TypeScript

Paste a JSON sample to generate TypeScript interfaces. Nested objects become their own named interfaces, arrays infer their element type from the first item, and null values map to the null type. Everything runs in your browser.

Overview

Generate TypeScript interfaces from JSON

When you consume an API, you usually start with a JSON response and need matching TypeScript types so the rest of your code is type-safe. Writing those interfaces by hand is tedious and easy to get wrong, especially for deeply nested objects. A JSON to TypeScript generator reads a sample and produces the interfaces for you.

ToolHub JSON to TypeScript parses your JSON sample and infers types recursively. The root object becomes an interface Root, nested objects become their own named interfaces, arrays become typed arrays, and primitives map to string, number, boolean, or null. Everything runs in your browser.

Step-by-step

How to generate interfaces

  1. 1

    Paste a JSON sample

    Drop a representative JSON object into the input panel. One complete example with all the fields you care about is enough.
  2. 2

    Review the generated types

    The output panel updates live with an interface Root and any nested interfaces it needs, named after your keys.
  3. 3

    Copy into your project

    Click Copy and paste the interfaces into a .ts file. Rename Root to something meaningful for your domain.

Background

How type inference works

The generator walks your JSON value by value. Each object becomes an interface; the name is derived from the key that holds it, converted to PascalCase. Arrays are typed from their first element, so a list of objects produces a reusable element interface and a T[] type on the parent field.

Primitives and null

Strings become string, numbers become number, and booleans become boolean. A JSON null becomes the null type. Because a single sample cannot tell that a field is sometimes null and sometimes a string, you may want to widen types like that by hand afterward.

Empty arrays

If an array is empty in your sample, there is no element to inspect, so the field is typed as unknown[]. Add a representative item to the sample to get a precise element type, or refine it manually.

Use cases

When to use this tool

Typing API responses

Paste a sample response from a REST endpoint to get interfaces for your fetch and axios calls.

Onboarding to a new codebase

Turn an unfamiliar JSON payload into readable types so you can see its shape at a glance.

Config and fixture files

Generate types for JSON config or test fixtures to catch typos at compile time.

Mock data and prototypes

Quickly type hand-written mock objects while prototyping a feature.

Migrating from plain JavaScript

Seed your first TypeScript types from the JSON your app already passes around.

Documenting payload shapes

Share generated interfaces in a pull request or doc to explain a data structure.

Tips for better output

  • Use a complete sample. Include every optional field at least once so it shows up in the interface.
  • Avoid empty arrays in your sample. One element lets the tool infer a real element type instead of unknown[].
  • Rename Root to match your domain, such as User, Order, or ApiResponse.
  • Mark fields optional with a question mark by hand when the API can omit them.
  • Widen single-sample types to unions, such as string | null, where the real data varies.

Common questions

Why is everything required in the output?

A single JSON sample shows which fields are present, not which are optional. The generator marks every field as required. Add a ? to keys that the source may omit.

How are nested interface names chosen?

They come from the key that holds the object, converted to PascalCase. If two different objects would produce the same name, the tool appends a number to keep the names unique.

What happens with arrays of objects?

The first element defines the element interface, and the field is typed as that interface followed by []. The element name is derived from a singularized version of the field name where possible.

Can it handle a top-level array or value?

Yes. If the root is not a plain object, the tool emits a type Root = ... alias instead of an interface, with any nested object interfaces alongside it.

100% private

Privacy and security

Parsing and code generation happen entirely in your browser with JSON.parse and local logic. Your JSON sample is never uploaded or sent over the network.

Related tools

Frequently asked questions

How are the types inferred?

The tool reads your JSON values: text becomes string, numbers become number, true or false become boolean, objects become interfaces, and arrays become typed arrays.

Does it handle nested objects?

Yes. Nested objects get their own named interfaces so the output is clean and reusable.

Is my JSON sent anywhere?

No. Everything runs locally in your browser.