JSON to TypeScript
Interfaces, types and Zod schemas
No database: AIMRAN doesn't store your files, text or results
What is JSON to TypeScript?
If you work with TypeScript and get JSON data from an API, writing the types by hand is slow. This tool reads a JSON sample and automatically generates the interfaces or types that describe it.
It can also generate a Zod schema to validate that data at runtime, with the inferred type ready to use.
How to use it
- Paste your JSON or load the example.
- Choose the output: TypeScript or Zod schema.
- Select whether to declare it as an interface or a type.
- Enter the root type name and turn on “Add export” if you need it.
- Copy or download the generated code.
Advantages
- Generates interfaces, types or Zod schemas.
- Nested objects get their own named types.
- Merges array items and marks missing keys as optional.
- Free, with no sign-up.
Use cases
- Type an external API's response from a real sample without writing the interfaces by hand.
- Generate a Zod schema to validate at runtime the data that reaches a form or an endpoint.
- Create the types for a configuration file or a translations JSON.
- Detect optional fields by comparing several items of a sample array.
Technical details
The tool infers a structural type from the JSON value: string, number, boolean, null, objects and arrays. In arrays, the types of all items are unified; if they're objects, their fields are merged and keys missing from any item are marked optional (?), and if types differ a union is generated. Each nested object produces a declaration named after its key. Empty arrays are typed as unknown[]. In Zod mode, z.object, z.array, z.union and .optional() are generated, and type Name = z.infer<typeof NameSchema> is added. Keys that aren't valid identifiers are quoted.
Frequently asked questions
How do I convert JSON to a TypeScript interface?
Paste the JSON, choose TypeScript and “interface”, and copy the result. Each nested object gets its own interface.
What's the difference between interface and type?
For describing objects they're almost equivalent. Interfaces can be extended and merged; types allow unions and more complex types.
What is Zod?
A TypeScript library for validating data at runtime. The schema checks that data has the expected shape and the type is inferred from it.
How does it detect optional fields?
In arrays of objects, if a key is missing from any of the items, it's marked as optional.
Does it detect dates in the JSON?
No. JSON has no date type, so dates are generated as string.
Blog guides
Related tools
- JSON · Format, validate and minify
- YAML ⇄ JSON · Convert in both directions
- SQL formatter · Format or minify queries
- Minifier · CSS, JS and HTML
- Test data generator · Names, Spanish IDs, emails and more as CSV or JSON
- Base64, URL and HTML · Encode and decode in all three formats