/

JSON to TypeScript

Processed Client Side

Generate TypeScript interfaces or types from a JSON sample, with nested objects and arrays inferred.

JSON · 13 lines
13 lines · 195 chars
Length: 195Lines: 13Size: 195 BytesCursor: 1:1
TypeScript
TypeScript3 types
typescriptLength: 231Lines: 19Size: 231 BytesCursor: 1:1

Bookmark this tool now — skip the search next time you need it.

About JSON to TypeScript

This tool runs entirely in your browser. Whatever you paste is processed on your own device and is never uploaded, logged, or sent to any server.

Generate TypeScript interfaces or type aliases from a JSON sample, with nested objects and arrays inferred into their own named types. It goes further than mapping JavaScript types across: when an array holds objects with differing keys, fields missing from some elements are marked optional with a question mark, and fields holding more than one type become a union rather than being flattened to any. That means the generated types describe the variation actually present in your sample instead of quietly hiding it. Name the root type, choose interface or type, and paste the result into your codebase. Nested structures are hoisted into their own declarations named after the field that holds them, so what comes out is a set of types you can import individually rather than one deeply indented block.

Key features

  • Generates TypeScript interfaces or type aliases from any JSON sample
  • Nested objects become their own named types rather than inline structures
  • Fields absent from some elements of an array are marked optional with ?
  • Fields holding more than one type become a proper union instead of any
  • Array element types inferred from the elements, including unions of mixed arrays
  • Configurable root type name, which then drives the names given to every nested type
  • Nested types hoisted into separate declarations rather than inlined, so each one can be imported and referenced on its own
  • Parse errors reported with the line number, so a malformed sample is fixed in the input rather than debugged in the output
  • Toggle between interface and type declarations
  • Syntax-highlighted output ready to copy into your project
  • Runs entirely in your browser — API payloads are never uploaded

How to use it

  1. Paste a representative JSON sample into the input pane.
  2. Set the root type name to something meaningful for your domain.
  3. Choose interface or type.
  4. Click Convert and copy the generated TypeScript.

Tips & common mistakes

  • Paste the most complete and varied sample you have. Optionality is inferred from what is missing across array elements, so a single perfect record produces types where everything is required.
  • A field that is null in your sample cannot be typed usefully — null alone tells the generator nothing about what it holds when populated. Fix those by hand.
  • Generated types describe your sample, not your API contract. They are a fast starting point, not a guarantee, so validate real responses at runtime with Zod or a similar schema library.
  • Prefer interface for object shapes you expect others to extend, and type when you need unions or intersections. The toggle here makes it a one-click decision.
  • Dates arrive as strings in JSON, so they type as string. If your code parses them into Date objects, that conversion has to be reflected by hand.
  • For the same job in another language, JSON to Go generates structs and JSON to C# and JSON to Java generate classes.
  • An empty array in the sample carries no element type and becomes unknown[]. Include at least one populated element for every list you care about, or you will be typing that field by hand anyway.
  • Array element types are named from the singular of the field, so a posts array yields a Post interface. When the field name does not singularise cleanly, that is the name worth changing before you commit the file.

Related tools

Browse all 24 JSON tools

Frequently asked questions

9

Paste a representative JSON payload, set a root type name, and press Convert. The tool emits TypeScript interfaces (or type aliases) matching the structure, including nested objects and arrays.

Yes. Toggle the "interface (vs type)" switch to choose between generating interface declarations or type aliases.

When you provide a JSON array, any field that is missing from some elements is marked optional with a ? in the generated type.

Empty arrays become unknown[] because there is no element to infer from, and null values are typed as null. Provide a richer sample to get more precise types.

Optionality is inferred from fields missing across elements of an array. A single record, or an array where every element has every key, gives the generator no evidence that anything is optional.

A field that is null in your sample cannot be typed usefully, because null alone says nothing about what it holds when populated. Those are the fields to correct by hand.

They describe your sample, not your API contract. Use them as a fast starting point and validate real responses at runtime with Zod or a similar schema library.

Prefer interface for object shapes others may extend, and type when you need unions or intersections. The toggle makes it a one-click decision either way.

JSON has no date type, so dates arrive as strings and are typed accordingly. If your code parses them into Date objects, that has to be reflected in the types by hand.