JSON to TypeScript Converter

Paste a sample JSON payload — an API response, a config file — and generate the matching TypeScript interface, including nested objects, arrays and union types.

json-to-ts · runs locally

What this JSON to TypeScript converter does

Typing an API response by hand is tedious and error-prone — one typo in a field name and the compiler happily checks the wrong shape. This tool reads a sample JSON payload and generates the TypeScript interface for it: primitives typed as string, number and boolean, nested objects as inline object types, arrays from their element types, and mixed arrays as unions. Keys that aren’t valid identifiers are quoted automatically.

Paste a response from your API, name the interface, and paste the result straight into your codebase.

How to generate TypeScript from JSON

  1. Paste a representative JSON sample — ideally with every field populated.
  2. Set the interface name (defaults to Root).
  3. Click Generate TypeScript and copy the output.
  4. Refine by hand: mark optional fields with ? and narrow strings to literal unions where appropriate.

Getting better types from better samples

The generator infers from evidence, so richer samples produce richer types. Prefer a response where optional fields are present, arrays have at least one element, and nullable fields carry a real value (then add | null yourself). If you’re not sure the sample is even valid JSON, run it through the JSON Validator first — the error messages there are more precise.

Frequently asked questions

How are arrays typed?

From their contents: an array of strings becomes string[], and an array with mixed member types becomes a union like (string | number)[]. An empty array carries no type information, so it becomes unknown[] for you to refine.

Why did a field come out as null instead of a useful type?

The generator can only see your sample. If a field is null in the sample, its true type is unknown — edit it to the real type and make it nullable, for example string | null. Feeding a sample where every field has a representative value gives the best result.

Are nested objects typed inline or as separate interfaces?

Inline, as object literal types nested inside the root interface. That keeps the output self-contained and correct for any structure; if you prefer named sub-interfaces, extracting them in your editor is a quick refactor.

Can generated types replace hand-written ones?

They are a starting point. Generated types describe one sample — they can't know a field is optional, an enum, or a template union. Review the output, mark optional fields with ?, and tighten string fields into literal unions where the API contract defines them.