JSON to TypeScript Type Guards
Generates TypeScript interfaces plus runtime type-guard functions such as isUser(value): value is User from sample JSON, checking every property, nested object, array element, optional and nullable field.
Use the tool
Why this tool exists
A TypeScript type says what a response should look like, but at runtime a response from a server you do not control can be anything. A type guard is the plain way to check: a function that returns true only if the value really has the shape. Writing them by hand for nested payloads is long and boring. Paste a sample response and this tool writes the interfaces and the guard functions together, including checks for arrays, nested objects, optional keys and null. It means you can validate API data without adding Zod or another library. Guards reflect only your sample, so review them where real data may vary. The sample stays in the browser.
How to use it
- Paste a sample JSON response into the input box.
- Set the root name, for example User.
- Copy the interfaces and guard functions into your code and use the guard where you parse API data.
Frequently asked questions
What does a generated guard check?
That the value is a non-null object, that each required property exists with the right type, that arrays contain items of the right type, and that nested objects pass their own guards. Optional and nullable fields are allowed to be absent or null.
How does it find optional fields?
From arrays of objects: a key missing in some items becomes optional. A single object gives no sign, so every key is treated as required.
Should I use this instead of Zod?
Guards avoid a dependency and are fine for small shapes. For larger or evolving schemas, a library gives better error messages. The Zod converters on this site are the alternative.
Are the guards strict about extra properties?
No. The generated guards check the properties found in your sample, so a real response with extra keys that are not in the sample is not a reason for them to fail. Review the code if you need strictness.