io-ts schema will appear here...
What is JSON to io-ts Schema Generator?
io-ts is a runtime type validation library for TypeScript built on top of fp-ts, the functional programming toolkit for TypeScript. Unlike TypeScript interfaces that only exist at compile time, io-ts types are actual runtime values that can validate incoming data at application boundaries — API responses, form inputs, configuration files, and message queue payloads. Each io-ts type is a codec: it knows how to decode (validate and parse) incoming data and encode (serialize) it back. The library uses the Either monad from fp-ts to represent validation results, returning Right with the validated value on success or Left with a detailed error tree on failure. This tool inspects your JSON sample and generates the corresponding io-ts codec tree — including t.type for objects, t.union for mixed-type arrays, and t.array for lists — along with a t.TypeOf type extraction so you get both runtime validation and compile-time type safety from a single definition.
How to Use
- Paste a representative JSON sample into the left panel — the more realistic the data, the more accurate the inferred types
- Click "Generate" to produce the io-ts codec tree with import statements
- Copy the output into your TypeScript project and install both io-ts and fp-ts as dependencies
- Use Schema.decode(data) to validate incoming data — it returns an Either from fp-ts
- Extract the static TypeScript type with type MyType = t.TypeOf<typeof Schema> for compile-time safety
Why Use This Tool?
Tips & Best Practices
- io-ts requires fp-ts as a peer dependency — install both with pnpm add io-ts fp-ts
- Use Schema.decode(data) to validate — it returns Either<Errors, T> from fp-ts, not a thrown exception
- For optional fields, manually change t.string to t.union([t.string, t.undefined]) or use t.partial for entire objects
- Add refinements with t.brand for domain-specific constraints like positive integers or email-format strings
- Combine with io-ts-types for common patterns like DateFromISOString, NumberFromString, and UUID brands
Frequently Asked Questions
How are JSON types mapped to io-ts codecs?
JSON strings become t.string, numbers become t.number, booleans become t.boolean, null becomes t.null, arrays of a uniform type become t.array(T), arrays with mixed types become t.union([...]), and objects become t.type({...}). Nested objects are recursively converted into nested t.type structures.
When should I NOT use io-ts?
Avoid io-ts if your team is not comfortable with functional programming patterns (Either, Option, pipe). The fp-ts dependency adds conceptual overhead. For simpler projects, Zod offers a more straightforward API with similar runtime validation and type inference. io-ts shines in codebases already using fp-ts or where functional error handling is a requirement.
What is the difference between io-ts and Zod?
Both provide runtime validation with TypeScript type inference. io-ts is older and based on fp-ts functional programming patterns, returning Either<Errors, T> for validation. Zod is more modern with a simpler API, throwing errors or returning safe parse results. io-ts is preferred in functional programming contexts; Zod is easier for most developers.
How do I validate data with io-ts?
Use the decode method: const result = Schema.decode(data). The result is an Either type from fp-ts. Use isRight(result) to check success, then result.right for the validated data. For errors, result.left contains detailed error information. Example: import { isRight } from "fp-ts/Either"; if (isRight(result)) console.log(result.right);
Can I make fields optional in io-ts?
The generator creates exact types from the JSON sample. To make a field optional, change t.string to t.union([t.string, t.undefined]) or use the t.partial helper for objects: t.partial({ optionalField: t.string }). Review the generated schema and adjust as needed for your API.
Is my data sent to a server?
No. All schema generation happens entirely in your browser. Your JSON data is never transmitted to any server, logged, or stored. You can safely generate schemas from sensitive API responses or configuration data.
Real-world Examples
API response validation in Express
Generate an io-ts codec from an API response sample, then use it to validate incoming data at the boundary of your Express application.
{
"id": 1,
"username": "alice",
"email": "alice@example.com",
"active": true
}import * as t from "io-ts";
const Schema = t.type({
id: t.number,
username: t.string,
email: t.string,
active: t.boolean,
});
type SchemaType = t.TypeOf<typeof Schema>;Validating nested data with mixed-type arrays
Handle a JSON payload where an array contains different value types, producing a t.union codec for the array items.
{
"name": "Report",
"values": [42, "N/A"]
}import * as t from "io-ts";
const Schema = t.type({
name: t.string,
values: t.array(t.union([t.number, t.string])),
});
type SchemaType = t.TypeOf<typeof Schema>;