toolgarden.xyz
中文

JSON → TypeScript

Free online tool to infer and generate TypeScript interface types from JSON / JSONC / JSON5

Input JSON / JSONC / JSON5

TypeScript Types

TypeScript types will appear here

About this tool

JSON to TypeScript recursively infers interfaces from one concrete sample. Strings, numbers, booleans, and null map to their basic types; objects become named interfaces; arrays use the first element to infer their item type. A root object is named Root and an object inside a root array is named Item.

The result is a starting point inferred from data, not an API contract. Fields that only appear in other records, optional properties, and unions cannot be established reliably from one sample, so the declaration must be reviewed against real documentation.

How to use it

  1. Provide a representative sample

    Choose a response with complete fields and typical array items rather than empty arrays or null-only records.

  2. Generate declarations

    Nested interfaces are created recursively and keys that are not valid identifiers remain quoted properties.

  3. Refine against the contract

    Add optional marks, unions, date aliases, and alternate array members before copying the types into a project.

Input and output example

Note that every field comes out required, and a null value yields no real type; both usually need fixing by hand.

JSON sample
{
  "id": 7,
  "name": "kit",
  "tags": ["a"],
  "owner": null
}
Generated interface
interface Root {
  id: number;
  name: string;
  tags: string[];
  owner: null;
}

Supported range and limits

Output
TypeScript interface definitions inferred from a JSON sample
What it infers from
This one sample only. A field absent from the sample is absent from the type
Optionality
A single sample cannot show whether a field is optional, so everything is emitted as required and you mark ? by hand
Nulls
A field whose value is null yields no real type and usually needs widening to a union manually
Arrays
The element type comes from the first item; mixed-shape arrays need a hand-written union
A more reliable route
When the API has OpenAPI or JSON Schema, generate from the spec; sample inference is for getting started quickly

When you would use it

  • Starting integration with a new API

    Generate a basic model from a real response, then tighten it using the API contract instead of typing every field manually.

  • Documenting a legacy data model

    Turn untyped JSON configuration into readable interfaces that expose the nested shape.

  • Typing a third-party API that ships no types

    When all you have is documentation and a sample response, generate interfaces from the sample and then add optionality and unions from the docs.

What to know before you start

  • Only the first array item drives inference, so different later items do not automatically create a union.
  • Every observed object field is required because a sample cannot prove that a field may be absent.
  • Dates and UUIDs are JSON strings and are not promoted automatically to Date or branded types.

Related concepts

interface
A TypeScript declaration describing an object shape, including property names, types, and optionality.
type inference
Deducing a static type from concrete values; an incomplete sample necessarily produces an incomplete result.

Frequently asked questions

How do I generate TypeScript types from JSON?
Paste a JSON sample and the tool infers each field's type and emits a matching TypeScript interface you can copy straight into your project.
Does it handle nested objects and arrays?
Yes. Nested objects become separate interfaces, arrays infer their element type, and mixed values become union types.
Does it upload my API data?
No. Type inference runs locally in your browser, so the sample response is never uploaded.
Why is every field required?
One sample cannot show that a field is sometimes absent. The tool will not guess for you; mark optional fields with `?` yourself against the API documentation. That is exactly why sample inference is a starting point rather than a substitute for a spec.
What did a null value infer to?
Just null, because the sample gives no clue about the real type. Those fields usually need widening by hand to something like `string | null`, with the actual type coming from the docs or from more samples.