JSON to TypeScript
Turn an ordinary JSON sample into a readable TypeScript type alias. Inspect optional fields and mixed arrays, then copy the code or save a TypeScript file. The result describes observed structure and needs review; it does not validate future data at runtime.
Generate a type from JSON
- Paste one JSON document or load the example. Objects, arrays and primitive roots are accepted within the input limits. Comments, trailing commas, duplicate decoded keys and unpaired surrogates are rejected.
- Choose a root type name beginning with an ASCII capital letter, followed by letters, digits or underscores, up to 64 characters. Select two or four spaces and Generate type.
- Review the inferred fields and the sample-only qualification. Copy writes only the current code; Download TypeScript saves the same UTF-8 code as json-types.ts. Editing an input clears the old result.
How sample inference works
Primitive values become null, boolean, number or string types. Strings that look like dates, identifiers or numbers stay strings. Numeric tokens are classified without a value conversion; a number declaration does not promise lossless JavaScript numeric values.
Objects observed at the same context are pooled. A key is optional only when absent from some object in that pool. An explicit null is a value alternative, so optional and nullable mean different things. Empty objects also count when deciding whether a key is missing.
Arrays combine their observed elements recursively instead of becoming fixed-length tuples. Empty sibling arrays can use evidence from nonempty arrays at the same context. A pool with no elements becomes unknown[]; unrelated property paths do not share evidence.
The result is one exported type alias with inline nested types, quoted keys and stable formatting. An object pool with no keys uses an unknown-valued string index signature, which is permissive rather than exact-empty. No enum, discriminant, dictionary, date or UUID heuristic is applied.
Required, optional and nullable examples
In the example, the first object has id 1, label Ada and score null; the second has id 2 and score 4. Both id and score occur in both objects, so they are required. Label occurs once and becomes optional. Score combines null and number.
For an object whose left field is an empty array and right field contains 1, left stays unknown[] and right becomes number[]. For two corresponding items arrays, one empty and one containing 1, their shared element evidence instead produces number[].
Merging objects with kind ok and a value, or kind error and a message, produces a string kind and optional value/message fields. This loses their original relationship and can admit unseen combinations. Review such variants when your application needs a discriminated union.
Inference and size limits
One sample cannot establish every future field, missing value or variant. Generated declarations are compile-time types, not a runtime validator, exact object schema or proof that an API response is correct.
Input is limited to 256 KiB of UTF-8 text, depth 32 and 20000 records. Each JSON value counts once and every object member adds two more records. Output is limited to 1 MiB including formatting and its final newline. Limits reject the whole operation without a truncated result.
One leading BOM at the beginning is accepted. Decoded duplicate names and unpaired surrogates are rejected as interoperability restrictions. Number tokens may have at most 1024 characters and an exponent magnitude up to 100000; no bigint or numeric-precision guarantee is inferred.
The tool accepts JSON text and emits a type alias. It does not import files or URLs, execute samples, generate interfaces or runtime code, infer JSON Schema, translate keys, or install a compiler in your browser. Review the declarations in the TypeScript project where you will use them.
Samples and code stay in this page's workflow
The tool adds no sample upload, saved history, input-bearing link or telemetry containing JSON, keys, aliases or generated code. Copy and download happen only when requested. Existing site services, extensions and your later use of the generated code are outside that limited behavior claim.
JSON to TypeScript questions
Sources and references
- rfc-editor.orgRFC 8259JSON syntax and interoperability.
- typescriptlang.orgTypeScript: Everyday TypesTypeScript aliases, arrays and unions.
- typescriptlang.orgTypeScript: Object TypesObject properties and index signatures.
- typescriptlang.orgTypeScript: unknownThe unknown type for values without evidence.
- typescriptlang.orgTypeScript: Erased TypesType declarations do not provide runtime validation.
Sources reviewed 2026-11-15. Pooling, naming and limits are this tool’s policies.
Related tools
JSON to YAML
Convert JSON to YAML locally while preserving number text and quoted strings; copy or download the complete result.
Open tool — JSON to YAMLYAML to JSON
Convert one YAML document locally to JSON with exact number text and bounded aliases, then copy or download the complete result.
Open tool — YAML to JSONYAML Formatter
Format YAML indentation and copy the checked result while retaining comments and source details.
Open tool — YAML FormatterXML Formatter
Check namespace-aware XML 1.0, format or compact locally, and copy or download UTF-8 output.
Open tool — XML FormatterJSON Formatter & Validator
Check JSON syntax, format or minify locally, and copy or download output while preserving original data tokens.
Open tool — JSON Formatter & Validator