When an API returns JSON, your code needs to know its shape: which fields exist, which are numbers or strings, which can be null, and what nested objects look like. Writing those type definitions by hand is slow and error-prone. Generating them from a real example is quicker and catches details people miss. This guide explains how JSON-to-type generation works, what each language’s output looks like, and the limits you must check by hand.
How type generation works
A generator walks the JSON and infers a type for every value: strings, integers, floating-point numbers, booleans, null, arrays and nested objects. Each nested object becomes its own named type, usually named after its key (address → Address). When an array contains several objects, the generator merges them: a field present in some items but not others becomes optional, and a field that is sometimes a number and sometimes null becomes nullable.
[{ "id": 1, "email": "a@x.in", "phone": null },
{ "id": 2, "email": "b@x.in", "phone": "98200…", "vip": true }]
From these two records a generator can tell that phone is a nullable string and vip is an optional boolean — which is exactly the kind of detail that causes bugs when typed by hand from documentation.
What the output looks like
TypeScript
export interface Root {
id: number;
email: string;
phone: string | null;
vip?: boolean;
}
JSON to TypeScript generates interfaces or type aliases. Types are erased at runtime, so they document and check your code but do not validate incoming data.
Zod
export const RootSchema = z.object({
id: z.number().int(),
email: z.string(),
phone: z.string().nullable(),
vip: z.boolean().optional(),
});
export type Root = z.infer<typeof RootSchema>;
JSON to Zod gives you runtime validation and the TypeScript type from one definition — the safer choice for data from outside your system. Add refinements such as .email() or .min(1) by hand where you know more than the sample shows.
Go
type Root struct {
ID int64 `json:"id"`
Email string `json:"email"`
Phone *string `json:"phone,omitempty"`
Vip *bool `json:"vip,omitempty"`
}
JSON to Go adds the struct tags that map JSON keys to exported Go fields; pointers represent values that can be missing or null.
Python
JSON to Python produces dataclasses, Pydantic models (which validate at runtime) or TypedDicts (for type checkers such as mypy), with Optional[...] for nullable fields.
Java and C#
JSON to Java generates classes or records with Jackson @JsonProperty annotations; JSON to C# generates classes or records with System.Text.Json attributes and nullable reference types.
JSON Schema: types that any language can check
JSON Schema is a language-neutral description of a JSON document — required properties, types, string formats, minimum and maximum values. A schema inferred from a sample is a good starting point for API documentation (OpenAPI uses JSON Schema), for validating configuration files, and for contract tests between services.
What a generator cannot know
An example shows what was in one response, not everything that can be. Review the output for these gaps:
- Optional fields that happened to be present. If your sample always includes
discount, the generator marks it required. Feed several varied records, or edit by hand. - Empty arrays.
"items": []says nothing about item type; you will getunknown[]orany. - Enums.
"status": "paid"becomesstring; if only"paid" | "pending" | "failed"are possible, narrow it. - Integers vs decimals. A price of
100looks like an integer; if it can be99.5, use a floating-point or decimal type. For money, consider integers in the smallest unit (paise, cents) or a decimal type. - Dates. JSON has no date type; ISO strings such as
"2026-10-29T10:00:00Z"are generated as strings. Convert them at the boundary. - Large IDs. 64-bit integers lose precision in JavaScript; keep them as strings in TypeScript.
A sensible workflow
- Collect two or three real responses, including one with missing or null fields, and combine them into an array.
- Generate types in your language.
- Rename the root type and any awkward generated names.
- Tighten enums, dates and money fields by hand.
- For data you do not control, validate at runtime (Zod, Pydantic, JSON Schema) rather than trusting a static type — see What Is JSON Schema? for contract design and How to Format and Validate JSON for checking sample payloads first.