JSON to TypeScript
Turn a JSON sample into typed interfaces and schemas.
- Local by default
- No signup
- AI is opt-in
Generated code
Paste a JSON response and get TypeScript you can drop into a project — interfaces, a single type alias, or a Zod schema for runtime validation. Arrays, nested objects and optional fields are inferred from the sample, and the sample never leaves the tab.
What the generator can infer
The input is parsed with JSON.parse, then walked as a tree. Strings become string, numbers become number, booleans become boolean and null is kept as its own type. Objects become interfaces or object types, and arrays become arrays of the merged item shape.
Arrays of objects are the interesting case. When two items have different keys, the generator merges them: a key present in every item is required, and a key missing from at least one item is marked optional with ?. That is the only optional-field signal a sample can provide, so it is a heuristic rather than a contract.
- An empty array has no item to inspect, so it becomes
unknown[]. - An empty object becomes
Record<string, unknown>rather than an interface with no members. - A key whose values disagree across array items becomes a union, such as
string | number. - Keys that are not valid identifiers are quoted instead of renamed.
Interface or type alias
Both options produce the same structural types. interface declarations are hoisted and named, which reads better for nested objects and supports declaration merging later. A type alias inlines nested shapes into one declaration, which is easier to copy into a single file.
Neither form emits runtime code. TypeScript erases both during compilation, so choosing between them has no effect on bundle size or behaviour. Pick the one your codebase already uses and move on.
The readonly option marks every generated property and array as readonly. It is a compile-time promise only: nothing prevents a value from being mutated at runtime by code that ignores the types.
Types are not validation
A generated interface describes what the sample looked like, not what the server will send tomorrow. TypeScript disappears at runtime, so a response missing a required field still compiles and still crashes when you read it. If the data crosses a trust boundary, validate it.
The Zod tab produces a schema that performs that validation. The generated code imports z from Zod, builds an object schema with the same optional fields, and ends with z.infer so the static type stays in sync with the schema. Zod is a runtime dependency, so this output is only useful in a project that already has it.
Even a schema generated from one response only proves that this response was valid. Fields can be missing, renamed or retyped later. Treat the output as a starting point for review, not as documentation of an API you do not control.
Why optional detection is a guess
Optionality is a property of the API contract, and a single response cannot reveal it. The generator uses one honest signal: if an array contains objects and a key is absent from some of them, the key is probably optional. If your sample has only one item, every key looks required because there is no counterexample.
If you know a field is sometimes absent, add ? yourself. If you know it is always present, remove any ? the merger added. The generated file is meant to be edited; that is faster than building a UI for every possible inference rule.
A practical workflow
- Paste a real response, not a hand-written example. Real payloads contain the nulls and odd keys that break assumptions.
- Set the root name to the domain concept, such as
UserorInvoice, instead of leavingRoot. - Copy the output into your types file and run the compiler. Fix the optional markers you disagree with.
- If the data is untrusted, switch to the Zod tab and use the schema at the boundary where the response is parsed.
Frequently asked questions
Can I paste a real API response?
Yes. The sample is parsed and converted in your browser, with no upload request. Generated type names mirror your payload, though, so review the output before committing it if field names contain internal identifiers.
Why was one of my fields marked optional?
Optional detection compares objects inside an array. If at least one item is missing that key, the merged type marks it optional. With a single sample object there is no comparison, so every field is treated as required.
Can it generate Zod, Valibot or Java classes?
This page generates TypeScript declarations and Zod schemas. Zod is the only runtime validator included because it is common and its inferred types match the schema. Other targets would need a different generator.
Does it detect dates or UUIDs?
No. A date in JSON is a string, and guessing from the format would produce types that lie when a different string arrives. If you need branded or validated types, add them during review or encode the rule in the schema.
What happens to duplicate keys or very large files?
JSON.parse keeps the last value for a duplicate key, so the generated type reflects that value. Large files are limited by browser memory; a few megabytes is fine, but this is not a streaming parser for gigabyte exports.
Is the readonly option deep?
Yes. Every generated property is prefixed with readonly, and generated array types are readonly arrays. Nested objects are covered because they are generated by the same rule.