JSON to TypeScript

Turn JSON into TypeScript interfaces or types, with export, optional and readonly options. A free JSON to TypeScript converter that runs in your browser.

Runs in your browser. Your data never leaves your device.

How to use the JSON to TypeScript

  1. Paste a sample JSON response.
  2. Choose interface or type and set the root name.
  3. Tick export, all fields optional or readonly if you need them.
  4. Copy the types into a .ts file.

About this tool

Paste a JSON sample and get a TypeScript interface, or a type alias, for every object in it. Nested objects get their own named type, arrays become typed arrays, and a field that is missing from some items of an array is marked optional. Tick export, all optional or readonly to match your code style. Your JSON never leaves your browser.

Creating a TypeScript object from JSON

JSON.parse and response.json() return any, so tell TypeScript what to expect with the interface made here. Types only exist while compiling: if the API can send something else, check the data at run time too.

interface User {
  id: number;
  name: string;
  email?: string;
}

const user = JSON.parse(text) as User;

// or straight from fetch
const res = await fetch('/api/users/1');
const user2: User = await res.json();

Importing a .json file

To import a JSON file in TypeScript, turn on resolveJsonModule in tsconfig.json. TypeScript then works out the file's type from its contents, so config files need no interface.

// tsconfig.json
{
  "compilerOptions": {
    "resolveJsonModule": true,
    "esModuleInterop": true
  }
}

// app.ts
import settings from './settings.json';
console.log(settings.title);

Is there a JSON type in TypeScript?

No. JSON.parse returns any and JSON.stringify accepts any. If you want one, a short recursive type covers every valid JSON value.

type Json = string | number | boolean | null | Json[] | { [key: string]: Json };

Checking JSON at run time with zod

An interface doesn't check data from a server. zod, a package on npm, does: describe the shape once, parse the data, and get the TypeScript type from the same schema. Start from the interface made here and rewrite it as a zod object.

import { z } from 'zod';

const User = z.object({ id: z.number(), name: z.string(), email: z.string().optional() });
type User = z.infer<typeof User>;

const user = User.parse(JSON.parse(text));   // throws if the data doesn't match

JSON Schema, VS Code and other languages

If you have a JSON Schema instead of a sample, the json-schema-to-typescript package on npm turns it into types, and VS Code extensions such as Paste JSON as Code do this page's job inside the editor. The JSON tools below make C#, Java, Kotlin and other classes from the same sample.

Frequently asked questions

How do I import a JSON file into TypeScript?

Set "resolveJsonModule": true (and usually "esModuleInterop": true) in tsconfig.json, then import data from './data.json'. TypeScript types the data from the file.

Does TypeScript have a JSON type?

No built-in one. JSON.parse returns any. Use an interface made here for known data, or a recursive Json type for any JSON value.

How do I convert JS code to TypeScript?

Rename the file from .js to .ts (or turn on allowJs and checkJs to go file by file), add types for your data first, often starting from interfaces like these, then fix the errors the compiler shows.

How can I create a TypeScript object from JSON?

Parse it and give it a type: const user = JSON.parse(text) as User. To be sure the data really matches, validate it with a schema library such as zod.

Interface or type?

Either works for JSON data. Interfaces can be extended and merged; type aliases can also describe unions. Pick one and use it across the project.

Is my JSON sent anywhere?

No. The types are made in your browser.

More json tools

JSON and code models

All 31 free tools →
JSON tool · Runs in your browser

JSON Formatter

Format JSON online: beautify messy JSON with 2 or 4 spaces or tabs, sort keys, minify it to one line and find errors. Free, runs in your browser.

Free · No sign-upOpen tool
JSON tool · Runs in your browser

JSON Validator

Check if JSON is valid and see the exact line and column of any error, with a hint on how to fix it. A free online JSON validator in your browser.

Free · No sign-upOpen tool
JSON tool · Runs in your browser

JSON to Kotlin Data Class

Paste JSON and get Kotlin data classes for Android, with kotlinx.serialization, Gson or Moshi names, nullable fields and defaults. Free, in your browser.

Free · No sign-upOpen tool
JSON tool · Runs in your browser

JSON to Dart (Flutter)

Convert JSON to null-safe Dart model classes for Flutter, with fromJson, toJson and an optional copyWith. Free, no sign-up, runs in your browser.

Free · No sign-upOpen tool