πŸ”·

JSON to TypeScript Generator

Paste any JSON payload and instantly generate strongly-typed TypeScript interfaces or Zod validation schemas. Supports nested objects, arrays, optional fields, and union types.

JSON to TypeScript Generator

Paste any JSON payload to generate TypeScript interfaces or Zod schemas instantly.

Paste JSON β€” get TypeScript interfaces or Zod schemas in one click

You're consuming a REST API and the response is a 40-line JSON object with nested objects and arrays. You need to write TypeScript interfaces for every level of nesting, name each one correctly, handle the nullable fields, and deal with the mixed-type array. Instead of typing it all manually β€” which takes 15 minutes and you'll probably miss a field β€” paste the JSON here and get a complete set of exported interfaces in one click. Toggle to Zod mode if you also need runtime validation schemas.

Paste JSON into the left panel β€” the output updates automatically. The tool handles nested objects (generating sub-interfaces with PascalCase names), arrays (inferring element types), mixed-type arrays (producing union types), and null values (marking fields as optional). Click Copy to export the result directly into your codebase.

Who uses this generator

Developers typing API responses

A developer receives a 200-line JSON response from a payments API and needs TypeScript interfaces for every endpoint. Instead of manually writing each interface β€” which is tedious and error-prone β€” they paste a sample response and get a complete, correctly-nested interface in seconds. For 30 endpoints, that's 30 pastes instead of 30 minutes of manual typing.

Teams adding Zod validation to config files

A Node.js application loads configuration from a JSON file. A TypeScript interface gives compile-time safety, but Zod schemas add runtime validation β€” catching malformed config at startup instead of at an unpredictable point during execution. The Zod mode generates importable schemas with .nullable().optional() chains for null values and z.union() for mixed arrays.

Test fixture authors creating mock data

When writing integration tests, you need typed mock data that matches the API contract. Paste a real API response, generate the interface, and your test fixtures are type-safe. The generated types will catch mismatches at compile time β€” a typo in a field name or a wrong type becomes a TypeScript error instead of a flaky test.

How the generator infers types from your JSON

Basic type mapping

The generator reads each value in your JSON and maps it to a TypeScript type: strings become string, numbers become number, booleans become boolean, and null marks a field as optional with type string | null. Arrays are typed by looking at the first element β€” an array of objects becomes Item[], an array of strings becomes string[]. Objects trigger a new interface block with the property name converted to PascalCase.

Worked example: the sample payload

The sample JSON contains an id (number), name, email (strings), age (null), roles (array of strings), address (nested object with street, city, postcode), and active (boolean). The generator produces: an export interface Root with id: number, name: string, email: string, age?: string | null (optional because null), roles: string[], address: Address (a new sub-interface), and active: boolean. The Address interface contains street: string, city: string, postcode: string.

What happens with arrays

If all elements in an array have the same type, it generates a typed array β€” string[], number[], or Item[] for objects. If the array mixes types (like [1, "hello", true]), it collects the unique inferred types and produces a union: (string | number | boolean)[]. Empty arrays produce unknown[] because there's nothing to infer the element type from.

Zod mode: runtime validation

In Zod mode, the same traversal generates z.string(), z.number(), z.boolean() for primitives. Null values produce .nullable().optional(). Arrays become z.array(). Objects become z.object() with nested schemas exported as standalone constants. The output includes an import statement and a type inference line: export type Root = z.infer<typeof rootSchema> β€” giving you both runtime validation and compile-time type safety from a single source.

Edge cases

Top-level arrays are wrapped in a root type alias (type Root = Item[]) rather than an interface, because TypeScript doesn't allow interfaces to extend arrays. Nested object names are derived from the property key using PascalCase β€” address becomes Address, shipping_info becomes ShippingInfo. The generator handles any nesting depth β€” the recursion terminates naturally when it reaches primitives or empty arrays.

Frequently Asked Questions

Does it handle deeply nested JSON?

Yes. The generator recurses through nested objects and creates sub-interfaces (or nested Zod objects) for each level. Interface names are derived from the property name using PascalCase β€” a field called shipping_info becomes ShippingInfo. There is no practical depth limit; the recursion terminates when it reaches primitives (string, number, boolean, null) or empty arrays.

What happens with arrays of mixed types?

If an array contains elements of different types, the generator collects the unique inferred types and produces a union type β€” for example, (string | number)[] for a mixed array. In Zod mode, it uses z.union() with the detected member schemas. The deduction is based on the actual runtime types present in the elements.

Why use Zod instead of a TypeScript interface?

TypeScript interfaces are erased at compile time β€” they provide no runtime safety. Zod schemas validate data at runtime, which matters for API responses, form inputs, user uploads, or any external data where the shape can't be guaranteed. Both are complementary: Zod schemas infer TypeScript types via z.infer, giving you both runtime validation and compile-time type safety from a single source.

Can I paste a JSON array at the top level?

Yes. If the top-level value is an array, the generator wraps it in a root type alias. For interfaces it produces type Root = Item[]; for Zod it produces z.array(itemSchema). The item type is inferred from the array elements.

How does the tool handle null values?

JSON null is not a TypeScript primitive. When the generator encounters null, it marks the field as optional using the ? syntax and types it as string | null (the most common intent). In Zod mode, null values produce .nullable().optional(), allowing both null and undefined at runtime.

Is the generated code production-ready?

The output follows TypeScript and Zod best practices: exported interfaces, proper nesting, correct union types, and valid Zod schema composition. For production use, review the generated types against your actual data contracts β€” the tool infers from the shape of the sample JSON you provide, so ensure your sample represents all possible response shapes.

What if my JSON has duplicate keys?

JSON parsers handle duplicate keys by keeping the last value. If your JSON has {"name":"Alice","name":"Bob"}, the parser sees only name: "Bob". The generator will produce name: string β€” it can't detect the duplicate because the parser already resolved it. For most real-world JSON (from APIs or config files), duplicate keys aren't an issue.

jsontypescriptzodinterfaceschemadevelopertypevalidationjavascript

More Tools You Might Like

βœ…JSON Payload Schema Validator
πŸ“‹CSV to JSON & XML Data Converter
πŸ—„οΈSQL Schema Syntax Translator
πŸ”“JSON String Un-escaper
πŸ§ͺMock JSON Generator
πŸ”‘JWT Debugger