JSON to TypeScript Interfaces: Optional Fields and Nulls
Last updated: · 5 min read
Before you can use an API response in TypeScript, you need types that describe its shape. Writing dozens of fields by hand is tedious and invites typos, so most developers generate the types from a sample response instead. The JSON to TypeScript Interface tool walks the JSON you paste and writes an export interface for every object it finds. A sample is not a contract, though: it cannot tell you what type a field has when the sample happens to contain null, or which fields are sometimes missing altogether. This guide covers the inference rules, the difference between interface and type, and the places where the generated code needs a human edit. The output in the example is the tool's real result.
Formula
Rules:
1. The text is parsed with JSON.parse and a type is derived for every value: string, number, boolean, null, array or object.
2. The root object becomes an export interface with the name you supply (converted to PascalCase). If the root is not an object, an export type is written instead.
3. Each nested object becomes its own interface, named after its key in PascalCase (address → Address). A number is appended when names collide.
4. All items of an array are merged into one element type: different primitives form a union (string | number) and objects are folded into a single interface. An empty array becomes unknown[].
5. A key that is missing from some of the objects in an array is marked optional with ?.
6. Where null is seen, the type null is written; if the field has a value in other items, the result is a union such as string | null.
7. Keys that are not valid identifiers are quoted ("b-c").
Input (root interface name: User):
{"id":1,"name":"Ali","tags":["a","b"],"address":{"city":"Ankara","zip":null},"orders":[{"no":7,"paid":true},{"no":8,"note":"gift"}]}
Output:
export interface User {
id: number;
name: string;
tags: string[];
address: Address;
orders: Orders[];
}
export interface Address {
city: string;
zip: null;
}
export interface Orders {
no: number;
paid?: boolean;
note?: string;
}This is a type-inference algorithm; there is no formula. JSON (RFC 8259) does not distinguish integers from decimals, so both become number, and dates, UUIDs or fixed sets of values all show up as plain string. Interface names are derived from the key and plural names are not singularised: the key orders produces the interface Orders and the type Orders[].
What the inputs mean
- JSON
- The sample JSON to infer types from. Paste a real API response; the more records it contains and the more they vary, the more accurate the optional fields and unions will be.
- Root interface name
- The name of the outermost interface. Root is used if the field is empty. Whatever you type is converted to PascalCase: user profile → UserProfile.
Worked example: Generating interfaces from a user response
- With User as the root interface name, the root object becomes export interface User. id is a number, name is a string, and tags, holding two strings, is string[].
- address is a nested object; the interface Address is generated from the key and referenced in User as address: Address.
- zip is null in the sample, so its type is written as null. The tool cannot know whether a filled-in value would be a string or a number; you will most likely need to change it to string | null.
- The two objects in the orders array are merged into one interface. The name is not singularised: the interface is called Orders and the field type is Orders[].
- no is present in both items and is therefore required. paid appears only in the first and note only in the second, so both are marked optional: paid?: boolean and note?: string.
Result: The output consists of three interfaces: User, Address and Orders. Before using it, replace the line zip: null with the real type and consider renaming Orders to Order.
🟦 JSON to TypeScript Interface
Free, no sign-up; your data stays in your browser.
Open the calculator →How to read the result
Treat the generated code as a starting point. One sample only describes what that sample contains: a field that is always filled in here may be optional in reality, and "status":"paid" comes out as string even though the API may only ever send "paid" | "pending". A practical way to catch more cases is to put several different responses into one array and convert that; the tool merges the items and marks missing fields with ?. Remember too that TypeScript types are erased at compile time: an interface does not check that the data arriving at runtime really has that shape. This is general information.
Common mistakes
- Leaving a line such as zip: null untouched. The field then accepts nothing but null, and the compiler complains as soon as real data contains a string.
- Treating ? and null as the same thing. note?: string says the field may be absent; note: string | null says it is present and may hold null. In JSON these are different situations.
- Trusting types generated from a single record. With one item in the array, no field can ever look optional.
- Expecting a type from an empty array. "items":[] gives items: unknown[]; the element type has to be filled in by hand.
- Believing the interface validates the data. A cast such as as User performs no check at runtime; validate anything that comes from outside separately.
- Assuming identical keys share one interface. An address key that occurs in two different places yields two interfaces, Address and Address2; merge them by hand if the shapes match.
Frequently asked questions
Why does the tool emit interface rather than type?
For object shapes the two are largely interchangeable. According to the TypeScript Handbook, the key distinction is that a type cannot be re-opened to add new properties, whereas an interface is always extendable. When the root value is not an object the tool has to use type: a root-level array produces a line such as export type Row = RowItem[];
Can I rename Orders to Order?
Yes, by hand. The tool does not singularise names because doing so goes wrong for irregular plurals and non-English keys. Your editor's rename command updates every reference at once.
How are optional properties detected?
Only within arrays. The tool compares the objects in an array and marks any key that is not present in all of them with ?. A single object carries no evidence about which fields might be missing.
Are integers and decimals distinguished?
No. TypeScript has one number type for both and JSON makes no distinction either. Very large identifiers are safer when the API sends them as strings.
Is my JSON uploaded to a server?
No, the inference runs in your browser. It is still good practice to remove access tokens from a response before pasting it anywhere.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.