AltUtils

JSON to TypeScript

Paste JSON and instantly generate TypeScript interfaces or type aliases with nested types, optional fields and arrays. Free and offline.

CategoryDev Core
Sheet09 / 42
ProcessingIn your browser
Output
Declare as

TypeScript · 5 types

export interface Root {
  id: number;
  name: string;
  email: string;
  isActive: boolean;
  createdAt: string;
  roles: string[];
  profile: Profile;
  orders: Order[];
  "last-login": null;
}

export interface Profile {
  bio: null;
  avatarUrl: string;
  social: Social;
}

export interface Social {
  github: string;
  twitter: null;
}

export interface Order {
  orderId: string;
  total: number;
  items: Item[];
  coupon?: string;
  shippedAt?: string;
}

export interface Item {
  sku: string;
  qty: number;
  price: number;
}

What is the JSON to TypeScript Converter?

This converter reads a JSON sample — an API response, a config file, a database export — and writes TypeScript type definitions that describe it. Nested objects become their own named interfaces (a "profile" key becomes a Profile interface), arrays of objects are merged into a single item type whose name is singularized from the key ("orders" → Order), and structurally identical objects share one declaration instead of being duplicated.

Merging is where most generators fall short. When array items differ, keys that are missing from some items are marked optional (?), keys whose values have different types become unions such as string | number, null values add | null, and empty arrays become unknown[] until a sample contains data. Property names that aren't valid identifiers ("first-name", "2fa", keys with spaces) are quoted automatically, and names that would shadow built-in globals like Date or Error get a suffix.

Switch the output to Zod to get runtime validators instead: z.object() schemas ordered so dependencies are declared first, with .optional(), .nullable() and z.union() mapped from the same analysis, plus an exported z.infer type for each schema. The JSON is parsed by a strict parser that reports the exact line and column of syntax errors such as trailing commas, single quotes or comments. Everything runs locally in your browser.

Frequently asked questions

Should I use interface or type?
For object shapes both work the same in day-to-day code. Interfaces can be extended and merged by re-declaration and show their names in error messages; type aliases can also express unions and mapped types. Many style guides default to interface for objects — pick whatever your codebase uses. A top-level array or primitive is always emitted as a type alias because interfaces can't describe those.
How are optional properties detected?
Only from arrays: if a key appears in some objects of an array but not in others, it becomes optional. A single object can't tell the generator what might be missing, so its keys stay required. Choose “Make all optional” for partial payloads such as PATCH bodies, or “All required” to disable detection.
Why is a field typed as unknown[] or null?
The sample only contained empty arrays or null values for that key, so there was nothing to infer the real type from. Add a sample with real data (in an array, the values are merged automatically) or edit the type by hand — for example change bio: null to bio: string | null. unknown[] is used instead of any[] because it forces a type check before use.
Does it detect dates, enums or integers?
No. JSON has no date or integer types, so "2026-11-08" is a string and 42 is a number. Narrowing strings to literal unions or Date objects depends on your API contract, so it's left to you; in the Zod output you can add .datetime(), .int() or z.enum() where appropriate.
Is the generated Zod schema compatible with Zod 4?
Yes. It only uses APIs shared by Zod 3 and 4 — z.object, z.array, z.union, z.record with an explicit key schema, .optional() and .nullable() — and imports { z } from "zod".
Is my JSON uploaded anywhere?
No. Parsing and code generation happen entirely in JavaScript in your browser tab, so it's safe to paste private API responses or production data. Large inputs of several megabytes are handled without blocking typing.