JSON to TypeScript
Paste a JSON sample and get TypeScript interfaces, instantly and entirely in your browser. Optional fields, nullability and nested types are inferred from the sample — and anything the sample cannot settle is reported rather than guessed.
Type definitions will appear here
Typescript interfaces from a sample
Generates export interface declarations, with ? on fields absent from any sample object and | null on fields that are ever null.
TypeScript numbers are IEEE 754 doubles, so an integer beyond 253−1 cannot be represented exactly. Those fields are flagged.
What a sample cannot tell you
Types are inferred from one sample. A field absent from your sample will be absent from the output, and a field that happens to hold only whole numbers will be typed as an integer even if it can hold decimals. Paste the widest sample you have. For a guarantee rather than an inference, start from a JSON Schema.
What TypeScript output looks like
Generated from two sample records, one of which has an extra field and a null. This is the actual output, not an illustration:
export interface Root {
id: number;
userName: string;
isActive: boolean;
score: number;
tags: string[];
address: Address;
lastSeen: string;
nickname?: string;
}
export interface Address {
city: string;
postcode?: null;
}
The three things that differ by language
Large integers
TypeScript numbers are IEEE 754 doubles. 9223372036854775807 cannot be represented, and by the time your code sees it the value is already 9223372036854776000 — JSON.parse did that, not the type. If the field is an identifier, type it string and fix the producer.
Optional against nullable
A field absent from some samples gets ?; a field that is ever null gets | null. The two are different things and are emitted separately, which matters under strictNullChecks.
Names that are not identifiers
Keys that are not valid identifiers stay quoted, so "user-name" is emitted as-is rather than renamed.