π· JSON to TypeScript
Paste JSON, get clean TypeScript interfaces instantly.
How to Use This Generator
Paste any JSON, name the root interface, and get TypeScript types with nested objects expanded into their own interfaces. Arrays become typed arrays, nulls become union types, and everything runs locally in your browser.
Paste valid JSON into the input box.
Set the root interface name and pick interface vs type plus export style.
Toggle optional, readonly, and null handling, then copy the output.
How Type Inference Works
Each JSON value maps to a TypeScript type: strings to string, numbers to number, booleans to boolean, null to null (or | null unions), arrays to the union of their element types plus [], and objects to named interfaces. Nested objects get PascalCase names derived from their property key, so address becomes Address. Empty arrays become unknown[] since there is nothing to infer from, and mixed arrays like [1, "a"] become (number | string)[].
JSON has no distinction between integers and floats, so all numbers become number. If you need integer semantics, refine the output manually. Dates arrive as strings; the generator cannot know a string is a date, so annotate those as Date or string based on how you parse them.
Options Explained
Optional mode is ideal for API responses where fields may be missing. Readonly suits state models you never mutate. The null union option keeps you honest about nullable fields instead of silently typing them as their non-null guess.
Worked Example
Given {"id": 1, "tags": ["a"], "address": {"city": "X"}}, the generator emits an Address interface with city: string, then a root interface with id: number, tags: string[], and address: Address. With export interface style and the root named User, the output drops straight into a .ts file.
Tips for Real APIs
Paste a real API response rather than a hand-written sample: real data reveals nulls, missing fields, and mixed types that samples hide. For large responses, generate once, then split the interfaces into separate files by domain. And remember that JSON keys with dashes or spaces become quoted property names, which are valid TypeScript but awkward to use.
Frequently Asked Questions
How do I convert JSON to a TypeScript interface?
Paste the JSON into this generator. It infers types for every value, expands nested objects into named interfaces, and outputs copy-ready TypeScript with your choice of interface or type syntax.
What does JSON null become in TypeScript?
By default, a union with the inferred type, like string | null. You can disable this to get stricter output, but real APIs do send nulls, so the union is usually the honest choice.
Should I use interface or type?
For object shapes they are nearly interchangeable. Interfaces support declaration merging and are conventional for public APIs; types handle unions and mapped types better. This tool generates either.
How are nested objects handled?
Each nested object becomes its own named interface using the PascalCase property name. Deeply nested structures produce a clean hierarchy of interfaces instead of inline anonymous types.
Why is my empty array typed as unknown[]?
An empty array has no elements to infer from, so the generator cannot know the intended type. Replace unknown[] with the real element type, like string[], before using the output.