N
NJHTools

JSON to TypeScript Interface

Paste JSON and get ready-to-use TypeScript interfaces for every nested object, with optional fields and unions inferred from your sample. Free, no signup, and generated in your browser so your data is never uploaded.

Language

16 lines · processed in your browser, nothing is uploaded

TypeScript output · root.ts

Generated code appears here.

Example: JSON to TypeScript

This JSON sample, with the root type named User, produces the TypeScript below using the default options.

Input JSON
{
  "id": 1024,
  "name": "Ada Lovelace",
  "email": "ada@example.com",
  "active": true,
  "score": 98.5,
  "tags": [
    "admin",
    "beta"
  ],
  "address": {
    "city": "London",
    "postcode": "NW1 6XE"
  }
}
Generated TypeScript
export interface User {
    id:      number;
    name:    string;
    email:   string;
    active:  boolean;
    score:   number;
    tags:    string[];
    address: Address;
}

export interface Address {
    city:     string;
    postcode: string;
}

How to convert JSON to TypeScript

  1. 1Paste a JSON object or array of records into the input.
  2. 2Select TypeScript as the output language.
  3. 3Name the root type, for example ApiResponse.
  4. 4Adjust options such as output style if available.
  5. 5Copy the interfaces into a .ts file in your project.

Interface or type alias?

For plain object shapes, interface and type are nearly interchangeable. Interfaces can be extended and merged by redeclaration, which suits public API models; type aliases are required for unions, mapped types and tuples.

A common convention is interfaces for objects and type aliases for unions. Pick one style per codebase and let your linter enforce it.

Optional vs null in TypeScript

An optional property (name?: string) means the key may be absent; a nullable one (name: string | null) means the key is present but may hold null. JSON APIs use both, and they are not the same under strictNullChecks.

The generator marks a field optional when it is missing from some records and adds null when it sees null values. Paste several records so both cases are visible.

Union inference and runtime safety

When a field holds different types across records, such as a number in one and a string in another, the output uses a union like number | string. Fields that only ever hold a handful of strings stay typed as string, so narrow them to literal unions yourself if they are really enums.

Interfaces disappear at compile time and do not validate incoming data. If the JSON comes from an untrusted source, also generate a Zod schema and parse responses at runtime.

Frequently Asked Questions

How do I convert JSON to a TypeScript interface?↓

Paste the JSON into the input, choose TypeScript, and copy the generated interfaces. Every nested object gets its own interface, and the root type uses the name you give it.

How are optional fields detected?↓

Types are inferred from the sample you paste. A field becomes optional only if it is missing from some of the objects, so paste an array with several representative records, or mark the field optional by hand after generating.

Does it handle nested arrays?↓

Yes. Arrays of objects become Foo[] with a Foo interface, and arrays of arrays become types such as number[][]. Arrays with mixed element types become arrays of a union.

Do generated interfaces validate data at runtime?↓

No. TypeScript types are erased when compiled. For runtime checks, use the JSON to Zod page and call schema.parse() on incoming data.

Is my JSON uploaded to a server?↓

No. The code generator (quicktype) runs entirely in your browser, so the JSON you paste never leaves your device. That makes it safe to use with internal API responses, though you should still strip secrets before sharing generated code.

Last updated: