JSON to Code

JSON to TypeScript

Paste a JSON sample. Get TypeScript types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.

Open the tool with TypeScript chosen

An example

This JSON:

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "joined": "2020-01-02T03:04:05Z",
  "tags": ["engineer", "writer"],
  "address": { "city": "London", "postcode": null },
  "scores": [9.5, 8, 10]
}

becomes this TypeScript:

export interface Root {
    id:      number;
    name:    string;
    active:  boolean;
    joined:  Date;
    tags:    string[];
    address: Address;
    scores:  number[];
}

export interface Address {
    city:     string;
    postcode: null;
}

With the reader and the writer

The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.

// To parse this data:
//
//   import { Convert, Root } from "./file";
//
//   const root = Convert.toRoot(json);
//
// These functions will throw an error if the JSON doesn't
// match the expected interface, even if the JSON is valid.

export interface Root {
    id:      number;
    name:    string;
    active:  boolean;
    joined:  Date;
    tags:    string[];
    address: Address;
    scores:  number[];
}

export interface Address {
    city:     string;
    postcode: null;
}

// Converts JSON strings to/from your types
// and asserts the results of JSON.parse at runtime
export class Convert {
    public static toRoot(json: string): Root {
        return cast(JSON.parse(json), r("Root"));
    }

    public static rootToJson(value: Root): string {
        return JSON.stringify(uncast(value, r("Root")), null, 2);
    }
}

function invalidValue(typ: any, val: any, key: any, parent: any = ''): never {
    const prettyTyp = prettyTypeName(typ);
    const parentText = parent ? ` on ${parent}` : '';
    const keyText = key ? ` for key "${key}"` : '';
    throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`);
}

function prettyTypeName(typ: any): string {
    if (Array.isArray(typ)) {

and 159 more lines. Open the tool to see the whole file.

What you need in your project

Nothing. An interface is part of the language.

An interface describes the shape at build time only. The compiler removes it, so nothing checks the data when the program runs. If the JSON comes from an API you do not control, a wrong field is found by a crash later, not by the compiler.

Turn off "Types only" to get the reader and the writer as well. They check the shape when the program runs, which is what you want at the edge of the program.

A Zod schema is the other answer, and this tool writes one. A schema checks the data and gives you the type from the same lines.

The trap to know about

A JSON number becomes `number`, which is a double. A number above 9007199254740992 loses its last digits before your code ever sees it.

The options you can change

These belong to TypeScript, so the row of switches changes with the language.

OptionWhat it doesDefault
Check at run timeVerify JSON.parse results at runtimetrue
Types onlyInterfaces onlyfalse
Tidy the namesTransform property names to be JavaScriptyfalse
Name the unionsExplicitly name unionsfalse
A union, not an enumUse union type instead of enumtrue
type, not interfaceUse types instead of interfacesfalse
A string, not a one-value enumUse string instead of enum for string enums with single valuefalse
Readonly membersUse readonly type membersfalse

Questions

How do I turn JSON into TypeScript?

Paste your JSON into the box, choose TypeScript, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.

Does my JSON go to a server?

No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.

What do I need to add to my TypeScript project?

Nothing. An interface is part of the language.

How does it know which fields are optional?

From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.

Other languages

JSON to Python JSON to Go JSON to Java JSON to C# JSON to Rust All the guides

Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.

Open JSON to Code