JSON to TypeScript

Generate TypeScript interfaces from a JSON sample โ€” optionals detected.

About this generator

Paste an API response, name the root type, copy the interfaces: nested objects become their own named interfaces, array elements merge into one type with missing-in-some-items fields marked optional, mixed arrays become unions, invalid identifiers get quoted.

Sample-based typing, honestly framed

The optional-detection is the part worth having: given an array where some items carry "email" and others don't, the generator merges every item's shape and emits email?: string โ€” the ? that hand-written types forget and runtime crashes remember. Nested naming follows sensible conventions (an items array yields an Item interface), and unions appear where the sample genuinely mixes types. Now the honest frame, which applies to every sample-based generator including the fancy ones: types describe YOUR SAMPLE, not the API's contract. A field that happened to be null types as null; a field absent from this response doesn't exist; an array with one item can't reveal optionality at all. Generate from the richest response available โ€” ideally several merged โ€” then spend two minutes reviewing against the API docs. It still beats transcribing by hand: a 40-field response types itself in a paste. Validate gnarly JSON first with the formatter; API responses arriving as CSV take a detour; and What JSON is, and where it trips people up covers the format itself.

Frequently asked questions

How are optional properties detected?

Across array items: a field present in some items but not others merges as optional (field?: type). It's the detail hand-written types miss and the main reason to generate from arrays with many items.

Why did a field come out as null?

It was null in your sample, and the generator won't guess what it would otherwise be โ€” that's sample-based typing being honest. Find a response where the field is populated, or adjust by hand.

Do nested objects get their own interfaces?

Yes โ€” each object becomes a named interface (an 'address' field yields Address; an 'items' array yields Item), so the output reads like hand-written code, not one giant inline blob.

Should I trust the generated types completely?

Trust them as a strong draft: they exactly describe your sample and only your sample. Two minutes against the API docs โ€” especially for nullable and rarely-present fields โ€” finishes the job.