JSON to TypeScript, Go and Python Types From a Sample
Short answer: paste a real API response into JSON to Code and it works out the structure and writes type definitions in TypeScript, Zod, Go, Python, C#, Java or Kotlin, or a JSON Schema. Rust, Swift, Dart, Python (Pydantic v2), PHP and Ruby are also offered. Arrays are analysed across every item, so the result covers each variation it sees.
How the types are inferred
- A field missing from some objects in an array becomes optional.
- A field that is
nullin some items becomes nullable. - Whole numbers become integers; any decimal makes the field floating-point.
- Fields with mixed types become unions (or a generic value in languages without unions).
- Empty arrays become arrays of an unknown type.
- Nested objects become their own types, and array item types are named in the singular (
ordersbecomesOrder).
A worked example
{"orders":[{"sku":"a","qty":1,"note":null},{"sku":"b","qty":2}]}
The TypeScript output:
export interface Order {
sku: string;
qty: number;
note?: null;
}
export interface Root {
orders: Order[];
}
note is optional because the second item lacks it. But the sample shows it only as null, so the tool cannot know it is really a string. If you know it is, change it to string | null by hand. A sample can only show what it contains.
Several samples
Put a line containing only --- between JSON samples to merge them into one set of types. A field missing from some samples becomes optional, and a field with different types in different samples becomes a union (or the nearest equivalent with a comment). A single valid JSON document is never split, and if one sample is invalid, the message names it. To check each sample first, use the JSON Validator; see JSON syntax errors. To see how two API responses differ before merging them, read comparing two JSON files.
More languages and renaming
Rust (serde), Swift (Codable), Dart, Pydantic v2, PHP and Ruby are generated with the usual helper methods. Reserved words, keys that are not valid names (spaces, leading digits, non-Latin letters) and clashing type names are renamed, and the original key is kept in the rename, alias or key string. Where a field can hold several types, the nearest equivalent is used, such as serde_json::Value, dynamic, Union[...] or mixed, with a comment listing the types seen.
Check the output before you rely on it
The generated code is a starting point. It uses common libraries (System.Text.Json, Jackson, kotlinx.serialization, Zod) and standard naming. The newer generators were tested against real compilers available when the page was built, but a sample cannot show every case, so compile the result in your own project. Very large numbers are read with 64-bit precision. To validate real data against a shape, use a schema with the JSON Schema Validator; for API specifications, see OpenAPI validation errors.
How to convert JSON to TypeScript with JSON to Code
- Open JSON to Code.
- Paste one or more samples, separated by
---. - Choose the language or JSON Schema.
- Copy the output, widen types the data does not reveal, and compile it.
Limits to know
- Types reflect only the sample you give.
- Missing or null fields are guesses about the real API.
- Everything runs in your browser.
Frequently asked questions
How do I generate TypeScript types from JSON? Paste a sample and choose TypeScript.
Why is a field optional? It is missing in some array items.
Why is a field typed as null?
It was only null in the sample.
Can I merge several samples?
Yes, separate them with ---.
Is the generated code production-ready? Treat it as a starting point and compile it.