Skip to main content

JSON to TypeScript, Zod and JSON Schema

Infer TypeScript interfaces, types, Zod schemas or JSON Schema from objects and arrays in sample JSON. Local generation in your browser.

  • Clipboard
  • Export

Embed this widget

Customize the result, check the live preview, then copy the code.

Preview

Embed code type

Code to copy

Responsive and automatically resized by Bethemesh.

Was this tool useful?

How does this tool work?

Turn sample JSON into TypeScript interfaces or type aliases, Zod schemas or JSON Schema. The generator traverses nested objects and every array entry instead of observing only the first one. Output is a starting point to review against your API: one sample cannot reveal every value a service may return. Inference stays on your device and generated code is not executed inside the form.

The engine identifies strings, numbers, booleans, objects, arrays and null. Different observed types can form a union. TypeScript numbers use number; date-like and email-like strings remain string without guessed business rules. An empty array reveals nothing about its future entries, so its item type is unknown.

Within an array of objects, observed properties are combined. A field missing from some entries can become optional. Force-required mode creates a stricter contract, even when some supplied examples would then fail it. A property present with null is not absent: nullability and optionality are separate facts.

Choose interface or type for TypeScript declarations, Zod for validation code to integrate, or JSON Schema for a schema document. readonly applies to TypeScript declarations rather than runtime object freezing. Zod output imports zod, which your project must provide, and declares child schemas before parent references.

Targets share inference without promising identical behavior in every context. TypeScript checks development code rather than automatically validating network responses. Zod requires an actual validation call in your application. Generated JSON Schema closes objects using additionalProperties: false, so handling extra keys needs an explicit decision.

How to use this tool

  1. Add a representative sample

    Paste or import valid JSON. Avoid unnecessary personal data: inference mainly needs structure and the different observed types.

  2. Choose root name and target

    Name the model and select output. Adjust missing-field handling and readonly when TypeScript output permits it.

  3. Review and integrate generated code

    Check unions, optional properties and dependencies before copying or exporting. Editing inputs or options hides old output. Compile or validate in your own project afterward.

Use cases

Draft API response models

Supply representative nested objects, variants and null values. Review proposed names and which fields the provider truly guarantees.

Prepare Zod validation

Generate basic schemas, then add application formats, limits and rules. A plain string is not automatically assumed to be a valid URL.

Compare a contract with several samples

Place comparable objects in an array to expose fields appearing only in some cases. Include known uncommon situations.

Examples

Create two interfaces for nested response data

With root name Response and interface output, user becomes a separate User model. id is number and name is string; types do not freeze the sample’s literal values.

Input

{"user":{"id":1,"name":"Ada"}}

Result

export interface Response {
  user: User;
}

export interface User {
  id: number;
  name: string;
}

Declare Zod dependencies before use

With root name Root and Zod output, ChildSchema precedes RootSchema, avoiding a reference to a constant before initialization.

Input

{"child":{"id":1}}

Result

import { z } from 'zod';

export const ChildSchema = z.object({
  id: z.number(),
});
export type Child = z.infer<typeof ChildSchema>;

export const RootSchema = z.object({
  child: ChildSchema,
});
export type Root = z.infer<typeof RootSchema>;

Tips and best practices

  • Include samples with omitted fields and null values. One fully populated response often hides optional properties.
  • A sample cannot prove an exhaustive enum or numeric bound. Add constraints using the service’s contract documentation.
  • Do not mistake readonly for runtime data protection: here it is a TypeScript check.
  • Keep large identifiers as strings when the API uses that representation. JSON is read with browser numeric types.

Sources and references

Frequently asked questions

Does a generated interface validate API responses?

No. TypeScript describes expected values during development. To check received data at runtime, add a validator and actually use its result before trusting the response.

How are optional properties detected?

Objects within one array are compared for keys missing from some entries. A single object always containing a field cannot reveal that the field may sometimes be omitted.

Why does a date or email become plain string?

A sample provides a string rather than proof of its business rule. The generator avoids turning appearance into arbitrary validation; add the required formats and constraints explicitly.

How does this differ from the standalone JSON Schema generator?

This tool offers several targets and naming or optional-field options. The standalone tool directly generates a schema and distinguishes observed integers. Both use the same shared inference engine.

Related tools

Tools similar or complementary to this one.

Similar tools

Generate a JSON Schema from a JSON example locally in your browser.

DevelopmentUse this tool

Validate a JSON document against a schema and locate every error precisely.

DevelopmentFeaturedUse this tool

Generate realistic fake datasets in JSON, CSV or SQL.

DevelopmentUse this tool