Favorite places of your favorite city
Home
Add to preferred sources on Google

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.

JSON sample

Inferred TypeScript

Generate a type to see it here.

This tool processes samples on this page; it does not upload or save them.

Generate a type from JSON

  1. 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.
  2. 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.
  3. 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

A property is optional when missing from some pooled object samples. Null is an explicit observed value and appears in the value type. A property can be required and nullable, optional without null, or both optional and nullable.

An array context with no element evidence uses unknown[]. Nonempty sibling arrays can supply evidence before that fallback is chosen. An object pool with no keys uses an unknown-valued string index signature, not an exact-empty object guarantee.

Numeric tokens are classified as number without evaluating their values. This avoids a value round-trip during inference, but the generated number type does not preserve arbitrary mathematical precision or infer bigint. Declared parser token and exponent bounds still apply.

No. Primitive values are widened, and corresponding object fields are merged. Relationships between a tag and other fields can be lost, allowing combinations not seen in the sample. Review those cases and model the intended variants explicitly in your project.

The root alias uses the documented capital-first ASCII name rule. Data keys remain separate: they are decoded and emitted as escaped quoted properties, including spaces, empty names, Unicode and prototype-looking names. Keys are not renamed or used as executable source.

No. Type declarations do not execute runtime checks. Successful compilation establishes a language/type contract under the tested compiler settings, not the completeness of a real API schema or the validity of future data.

Both use the current generated code only, without page headings or Markdown fences. Download saves json-types.ts as UTF-8 without a BOM and with one final newline. A new input or setting invalidates the old result before another export.

The tool adds neither a conversion-server upload nor saved sample history or a data-bearing URL. It performs this transformation in the page. Existing site services and extensions are outside that limited statement; use your normal care with private samples and generated property names.

Sources and references

Sources reviewed 2026-11-15. Pooling, naming and limits are this tool’s policies.

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 YAML

YAML 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 JSON

YAML Formatter

Format YAML indentation and copy the checked result while retaining comments and source details.

Open tool — YAML Formatter

XML Formatter

Check namespace-aware XML 1.0, format or compact locally, and copy or download UTF-8 output.

Open tool — XML Formatter

JSON Formatter & Validator

Check JSON syntax, format or minify locally, and copy or download output while preserving original data tokens.

Open tool — JSON Formatter & Validator
View all tools in this category