Skip to content
SmaftyToolsSmaftyTools

All tools →

JSON to TypeScript

Convert JSON payloads into clean TypeScript interfaces or type aliases instantly in your browser. Handles nested objects, arrays, and optional keys.

About JSON to TypeScript

JSON to TypeScript converts any valid JSON payload into clean, ready-to-paste TypeScript interfaces or type aliases directly in your browser. It automatically extracts nested objects into named interfaces, infers array element unions, and marks keys that only appear in some array items as optional (?).

Customize the root interface name, toggle between interface and type syntax, control export keywords, or mark all properties optional before copying or downloading your .ts file.

How does JSON to TypeScript infer nested interfaces and optional fields?

JSON to TypeScript parses your input locally with JSON.parse and recursively inspects every object, array, and primitive value. Whenever an object contains nested objects or an array of objects, the generator creates clean PascalCase child interfaces so your types stay readable and reusable instead of dumping deeply nested inline blocks. If an array contains multiple objects where a field like role appears in only some items, the tool automatically marks that property as optional (role?: string) and builds accurate union types for mixed primitives or nullable fields. Everything runs 100 percent client-side in your browser with zero uploads, making it safe to generate TypeScript models from internal REST or GraphQL API responses.

How to Use the JSON to TypeScript

  1. Paste any valid JSON response into the JSON input pane.
  2. Set your preferred Root name and toggle interface vs. type or optional properties.
  3. Copy the generated TypeScript definitions or download them as a .ts file.

Features

  • Automatic nested interface extraction with clean PascalCase naming
  • Smart optional property detection (?) across arrays of objects
  • Switch between export interface and export type in one click
  • 100% browser-side — zero uploads and instant .ts download

Examples

Generating types from a user list payload

Input

{"users":[{"id":1,"role":"admin"},{"id":2}]}

Output

export interface RootResponse { users: User[]; } and export interface User { id: number; role?: string; }

Frequently Asked Questions

Should I use interface or type for API responses?

Both work well in TypeScript. Interfaces are idiomatic for object shapes and support declaration merging, while type aliases are handy when you also need unions or mapped types. You can toggle between both with one checkbox.

How are arrays of objects with missing keys handled?

The generator merges all keys observed across the array and automatically adds the optional ? modifier to any key that is not present in every element.

Does this tool upload my JSON payload?

No. Type inference runs entirely in your browser's JavaScript engine with no server requests.

Can I validate my JSON before generating types?

Yes. If your JSON has syntax errors, the tool reports the exact parse error inline, or you can clean it first with the JSON Formatter.

JSON Formatter

Format, validate and beautify JSON online. Free JSON formatter with indentation options and key sorting — runs entirely in your browser.

JSON Diff

Compare two JSON objects semantically in your browser. Highlight added, removed, and changed JSON paths while ignoring key order and whitespace.

JSON Schema Validator

Validate JSON data against a JSON Schema and see exactly which fields fail, and why. Free online JSON Schema validator, runs client-side.

CSV to JSON

Convert CSV data to JSON online with header detection, custom delimiters and quoted-field support. Fast, free and private.