JSON to TypeScript
Infer a TypeScript interface from a JSON example, with optional fields worked out from an array.
Paste JSON on the left.
How it works
An API response is not a spec — it is one example a spec would satisfy. This reads that example and proposes a matching TypeScript interface, which is the fast 90% of writing types for a response you did not design.
Arrays are merged, not sampled from item 0
A naive version of this tool reads the first array element and calls it done, which is wrong the moment element 7
has a field element 0 does not. Every item is inferred and merged instead: a field present everywhere is required,
a field missing from at least one entry is ?, and a field whose type actually differs between entries
becomes a union.
Nested objects are inline, not hoisted
A nested object type appears as an inline literal rather than a second named interface. Deciding what to call an extracted type — and whether two "address" shapes at different paths are the same type or coincidentally identical — is a judgment call this tool does not make for you; copy a nested shape out and name it yourself if you want it separate.
This is shape, not runtime safety
The output is a type your editor will check the rest of your code against — it does not itself verify a response matches it. Where you need the response validated at runtime, not just typed at compile time, see the JSON → Zod Schema converter instead.
Example
[{"id":1,"name":"a"},{"id":2,"name":"b","tags":["x"]}] produces an interface where
id and name are required and tags?: string[] is optional, because only the
second entry has it.
Frequently asked questions
How does it decide a field is optional?
By merging every item in an array rather than reading item 0 and stopping. A field present in every entry is required; a field missing from at least one becomes optional. A single object with no array around it has no missing entries to compare, so nothing in it is inferred optional.
Why are nested objects not their own named interface?
Naming an extracted type is a real decision — what to call it, and whether two similarly-shaped objects at different paths are meant to be the same type or only coincidentally identical. This does not guess at that; it renders the nested shape inline, which TypeScript accepts anywhere a type is expected.
Does the output validate anything at runtime?
No. A TypeScript type is checked by the compiler and erased before your code runs, so it cannot itself reject a malformed response. Where you need that check to happen at runtime, the JSON to Zod Schema converter produces the equivalent schema as an executable validator.
Related tools
JSON Formatter & Validator
Format, validate and measure JSON, with errors located by line and column.
LocalJSON to Zod Schema
Infer a runtime-checkable Zod schema from a JSON example.
LocalJSON to JSON Schema
Infer a JSON Schema (2020-12) document from a JSON example.
LocalJSON Diff
Compare two JSON documents and see what actually changed, independent of formatting.
LocalCSV ⇄ JSON Converter
Convert CSV to a JSON array of objects, or back, with proper quoted-field parsing.
LocalJSONL Formatter & Validator
Validate JSON Lines file by line and convert it to a single JSON array.
Local