🧩

JSON to TypeScript

Generate TypeScript types from JSON: nested interfaces, array unions, optional fields, interface/type/zod output, camelCase/snake_case naming, and parse errors with line and column.
Type generator 📂 Developer 👤 ToolWorld 🏷️ v1.9.4 ⚙️ clipboard
Detects objects, arrays, numbers, booleans and null; array element types merge into unions
Output

    
Nested objects become their own interfaces; array items are named FieldItem
Type mapping
JSON valueTypeScriptNote
"abc"stringQuoted string
1 / 1.5numberIntegers and floats
truebooleantrue / false
nullnullEmitted as null or merged as optional
[1,2]number[]Element type + []
[]unknown[]Empty arrays become unknown[]
{"a":1}AExtracted as its own interface
[1,"a"]number | stringMerged into a union

How to Use

Overview

An API document hands over one JSON sample, the frontend needs TypeScript, the backend may need a Go struct, a Python dataclass or a Rust serde shape, and validation wants zod. This tool builds all six from the same sample so field names and optionality stay aligned instead of drifting into userName on one side and user_name on the other; nested objects become their own types, and arrays of objects are named after the field plus Item. Naming options apply to every dialect at once, so choosing camelCase for the client does not leave the Go struct behind in snake_case. The six dialects exist because one payload usually crosses stacks: a web client, a Go service, a Python analysis job and a Rust worker all read the same response, and each team writing its own model by hand is how optionality drifts apart. Generating from one sample keeps them together, and the output is deliberately plain, with no project-specific imports or decorators, so it drops into an existing file and can be shaped by the target repository linter rather than by this tool. One subtlety is that the same key can carry different shapes in different records, and merging them into a union is honest about the sample rather than hiding the conflict behind any; the Go output keeps the original key in its json tag so the wire format never changes.

Features

  • Six outputs: interface, type alias, zod schema, Go struct, Rust serde, Python dataclass
  • Four naming styles: keep as is, camelCase, PascalCase, snake_case
  • Optional-field inference treats keys absent from the sample as optional instead of inventing values
  • Nested objects are extracted into standalone types and arrays merge into unions
  • Errors point at line and column with a context snippet when parsing fails
  • Custom root type name, two or four space indent, copy-all and .ts download

How to use

  1. Paste JSON into the box, or press Example to load a sample with nesting and arrays
  2. Set the root type name, default Root, then choose indent and output form
  3. Toggle export and optional fields, and pick a naming style as needed
  4. Press Generate and read every type definition in the result area
  5. Use Copy all, or Download .ts to save the code as a file

Notes

  • Output comes from a single sample, so fields absent there are never inferred and the most complete response should be used; all six dialects share one field set and optionality.
  • When one field carries several types across records it becomes a union such as number | string, while an empty array can only yield unknown[] and needs manual filling.
  • An optional field emits key?: type, meaning the key may be absent rather than holding null, and date-like strings stay strings to avoid timezone assumptions.
  • zod output needs its package installed, Go output carries json tags, Rust needs the serde dependency added, and Python dataclass output rewrites illegal identifiers while noting the original key.
  • Parse errors report line and column to find a missing comma, numbers stay number without int64 or float splits, and type generation only means no validation or schema diff.

Related

⏫︎
💬