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.
| Option | What it does | Default |
|---|---|---|
| Check at run time | Verify JSON.parse results at runtime | true |
| Types only | Interfaces only | false |
| Tidy the names | Transform property names to be JavaScripty | false |
| Name the unions | Explicitly name unions | false |
| A union, not an enum | Use union type instead of enum | true |
| type, not interface | Use types instead of interfaces | false |
| A string, not a one-value enum | Use string instead of enum for string enums with single value | false |
| Readonly members | Use readonly type members | false |
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
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