JSON to TypeScript

Infer TypeScript types from JSON.

FreeNo accountOn your device

Runs in your browser. How to verify

Loading tool…

About this tool

Paste a JSON sample and get an exported TypeScript type with the right primitives, arrays and nested object types. The types come from the one sample you give it, so check optional and nullable fields yourself.

How to use

1
Step 1
Paste a representative JSON object (an API response works well).
2
Step 2
Press Run.
3
Step 3
Copy the type and rename the root type, which the tool calls tool.

How it works

The tool walks the sample and maps each value: strings to string, integers and decimals to number, booleans to boolean, null to null, arrays to T[] with all items merged (differing fields become optional, differing types a union), and nested objects to inline object types. Every property is required because a sample cannot show which ones are optional.

Worked example

An API response

Input
{"id":1,"name":"Ada","price":9.5,"tags":["a"],"owner":{"email":"a@b.c"},"active":true,"note":null}
Output
export type tool = { id: number name: string price: number tags: string[] owner: { email: string } active: boolean note: null }
Walkthrough
note is typed null because the sample value is null; widen it to string | null if it can hold text.

Limitations

  • Array items are merged: fields missing from some items become optional (?) and mixed item types become a union. Enums are not inferred.
  • Nested objects are inlined rather than given their own named types.
  • The root type is named tool.

Questions

How do I generate a TypeScript interface from JSON?
Paste a sample object and press Run. You get an exported type that you can paste into your code.
Does it create interface or type?
It creates an exported type alias, which can be used wherever an interface would be.
How does it handle null values?
A null in the sample is typed as null. Add | string or whichever type applies when the field can hold data.
Why is a field not optional?
A single sample cannot show missing keys, so all properties are generated as required.

Features

  • Nested objects and arrays
  • Integer and decimal to number
  • Exported type alias
  • Runs in your browser

Related resources

Guides and videos from around the web—opens on YouTube or the publisher’s site.

  • What Is A RESTful API?

    Traversy Media

    Most APIs exchange JSON—context for pretty-print and schema checks.

    Watch on YouTube
  • SQL Tutorial - Full Database Course

    freeCodeCamp.org

    JSON often lands next to SQL exports when debugging data pipelines.

    Watch on YouTube