Skip to content

JSON to TypeScript converter

Paste a JSON sample. Interfaces are written beside it.

Types come from one sample. A field that is always present in the sample is required, even if your API sometimes omits it.

Ctrl+Enter or ⌘+Enter runs this tool immediately.

Indent
JSON sample
TypeScript

Waiting for input.

Parsing stays in this browser. Input is not uploaded or saved.

How to use this tool

  1. Paste a JSON response or config file, or load the sample.
  2. Read the interfaces. Each nested object gets its own named interface.
  3. Copy or download the file, then rename interfaces and loosen types where your data varies more than the sample.

Supported formats and limitations

  • Types are inferred from the sample only. null becomes the null type, so widen it to string | null where you know the real type.
  • Objects in the same array are merged. A key missing from some of them becomes optional.
  • An empty array becomes unknown[].
  • Interface names come from key names. Plural keys are made singular for array items, such as deployments to Deployment.
  • Keys that are not valid identifiers are quoted.

Examples

An array of objects

Both items become one interface. canary appears in one item, so it is optional.

[{"id": 1}, {"id": 2, "canary": true}]

Mixed array

An array of strings and numbers becomes (string | number)[].

{"values": ["a", 1]}

Common errors and solutions

Expected double-quoted property name
The sample is not strict JSON. Remove trailing commas and comments, or copy the raw response body.

FAQ

Why is a field typed as null?
The sample only had null for it. Change it to the real type, such as string | null, after generating.
Can I get type aliases instead of interfaces?
Objects are written as interfaces. A root array or a primitive is written as a type alias named Root.
Is the JSON uploaded?
No. Types are generated in your browser.

Related tools

  • JSON formatter

    Validate JSON, then pretty-print it with 2 or 4 spaces or minify it. Invalid JSON produces an error and leaves the output empty.

  • JSON Schema validator

    Check a JSON document against a JSON Schema in the browser. Draft-07 and 2020-12 are supported, and remote $ref values are not fetched.

  • JSON validator

    Check JSON syntax in the browser. Errors give a line and column, and duplicate keys that JSON.parse would silently drop are reported.