JSON to TypeScript
Paste a JSON sample and get ready-to-use TypeScript types. Nested objects get their own named interfaces, arrays of objects are merged into one type, and keys that are missing from some items become optional. You can also get a JSON Schema or a Zod schema. Nothing is uploaded: it all runs in this page.
Letters, digits and underscores. Anything else is cleaned up.
Readonly and the interface or type choice only apply to TypeScript output.
- Declarations
- 4
- Input size
- 349 B
How it works
- •The page reads your JSON with a strict parser, then builds one type for every position in the data. Nested objects become separate named interfaces, named after their key in PascalCase (user_name becomes UserName). If two nested objects would get the same name, the second one is prefixed with its parent's name or numbered.
- •All items of an array are merged into one item type. For [{"id":1,"name":"a"},{"id":2}] you get id: number and name?: string, because name is missing from one item. Arrays that mix types become unions such as (string | number)[], and an empty array becomes unknown[] because there is nothing to learn from it.
- •The types are inferred from your sample only. A value that is null in the sample is typed as null, because one example cannot show what it is the rest of the time; edit those by hand (for example string | null). A key that is present in every item is treated as required even if the real API sometimes leaves it out. Keys that are not valid identifiers, such as "my key", are quoted.
- •Numbers are all typed as number, with no separate integer type. Whole numbers above 9,007,199,254,740,991 cannot be held exactly by JavaScript, so the page lists them and suggests sending them as strings. Dates are strings in JSON, so they are typed as string.
- •Nesting deeper than 30 levels is typed as unknown, and the input is limited to 5 MB. The JSON Schema output follows the 2020-12 draft and uses only $schema, type, properties, required, items and anyOf. The Zod output uses z.object, z.array, z.union, optional and nullable.
Questions
- Is my JSON uploaded?
- No. The conversion is done by code running in this page and the text is never sent anywhere. Copy and download also happen on your device.
- Why is a value typed as null instead of string | null?
- Because the converter only sees your sample. If the field is null in the sample, the only thing it knows is null. If the field can also be a string, paste several responses as items of one array (a field that is null in one item and a string in another becomes string | null), or change the type by hand. The option to make null properties optional is there for the same reason.
- How are optional properties decided?
- In an array of objects, a key is optional when at least one item does not have it. Outside arrays, every key in the sample is required. If one key holds different types in different items, you get a union of those types.
- Should I use the result as is?
- Treat it as a first draft. It describes the sample you pasted, not the whole API. Paste a few different responses inside one array to widen it, then review names and optional fields. For checking untrusted data at runtime, the Zod or JSON Schema output does that, while a TypeScript interface alone does not.