JSON to TypeScript
Paste JSON, such as an API response, and get TypeScript interfaces for it below, as you type: one for each kind of object, named after its key, with optional and nullable properties where the data shows them.
What the interfaces say
Numbers are number, true and false boolean, and lists arrays of what they hold (string[], (string | null)[]); an object keyed by ids or dates is a Record<string, …>. A key that some items lack ends in ?, a value that is sometimes null adds | null, and keys that aren't names, such as "first-name", are quoted. A list at the top becomes export type Root = Item[].
Types from sample data
Every item of a list, and every value of JSON Lines, is a sample of the same type: a key that some lack is optional, a value that is sometimes null is nullable, whole numbers mixed with decimals are decimals, and objects with the same keys share one type, named after the key they are under ("line_items" holds LineItem). An object whose keys are ids or dates, such as {"1001": {…}, "1002": {…}}, is a map. YAML, TOML, CSV and the other formats work too: pick their language above the editor.
Or the data as an object literal
Choose Data for the JSON written as a TypeScript constant (export const root = {…}), keys unquoted where they can be: a fixture for a test, or a mock. Integers too big for a number keep every digit as a bigint (123n).
Copy, download or switch
The TypeScript follows your typing, and a mistake in the JSON is pointed out first, with its line and column. Copy or Download (a .ts file) take it as it is. Above the code, switch between Types and Data, or pick another language; the Convert menu's Code item opens the one you picked last.
Private: nothing is uploaded
Formatting, checking and comparing all happen in your browser. Your documents are saved in the browser's own storage on this computer, not on a server, so they never leave your device.
With your permission, we count anonymous visits (which pages and tools are used), never what you type. See Privacy.
Questions
Interfaces or type aliases?
Interfaces, one per kind of object, which say the same as type aliases for JSON data. A list or a single value at the top gets a type alias, Root.
Are the values checked when the program runs?
No: interfaces are for the compiler only. To check data as it arrives, make a JSON Schema from the same sample with the JSON Schema generator, and validate against it.
Is my data uploaded anywhere?
Your documents aren't: everything runs in your browser, and they are saved on your computer, in the browser's storage for this site. With your permission we count anonymous visits, never their content.
More tools
- Formatter & Converter for JSON, YAML, XML, SQL
- JSON to Go Struct Converter
- JSON to C# Class Converter
- JSON to Java Class & Record Converter
- JSON to Python Dataclass, Pydantic & Dict
- JSON to Kotlin Data Class Converter
- JSON to Dart Model with fromJson & toJson
- JSON to Swift Codable Struct Converter
- JSON to Rust Struct Converter (serde)
- Free Online Diagram Maker & Editor
- Crypto Tools: Passwords, Keys, Hashes & JWTs
- Timestamp, Cron, Regex & UUID Utilities