How to convert JSON to TypeScript
- Paste a representative JSON sample (an API response works well) into the editor.
- Open the Code tab and pick TypeScript. Set the root interface name if you like.
- Copy the generated interfaces into your project.
How the types are inferred
The generator reads every element of an array and merges their shapes. Whole numbers and decimals both become number, mixed value types become union types such as string | null, and objects with the same structure share one interface instead of producing duplicates.
Tips for accurate types
Use a sample that includes the optional and nullable cases, ideally several records rather than one. A single example cannot reveal that a field is sometimes missing. Treat the output as a starting point and tighten it, for example by replacing string with a literal union.
From a JSON sample to useful types
Generated types are a starting point, not a specification. The generator reads the sample you give it and describes exactly that: it cannot know which strings are really dates, which numbers are really enums, or which fields might be missing in a response you have not seen. The more varied your sample, the better the result. If you paste an array of records, every record is merged into one interface, so a key that is absent from some of them becomes optional.
// Input
{ "id": 7, "name": "Ada", "email": null, "roles": ["admin"], "address": { "city": "London" } }
// Output
export interface Root {
id: number;
name: string;
email: null;
roles: string[];
address: Address;
}
export interface Address {
city: string;
}Optional, nullable and missing are three different things
In TypeScript, name?: string means the key may be absent. name: string | null means the key is always present but may hold null. A value that can be absent or null is written name?: string | null. APIs differ in which one they use, and the difference matters for code that checks `in`, `=== undefined` or `=== null`.
When your sample has a field that is null in every record, the generator can only say null, because it has no way to know what the real type would be. Replace it by hand with the type you expect, for example string | null, or give the generator a record where that field is filled in.
Dates, enums and other things JSON cannot say
JSON has no date type. A timestamp arrives as a string, so the generated type is string. If your code converts it to a Date, keep the wire type as string and convert in one place. The same applies to enums: a field that only ever holds one of a few strings is typed as string until you narrow it, for example to "active" | "disabled".
Large integers are another trap. JavaScript numbers are 64-bit floats, so ids above 9,007,199,254,740,991 lose precision when parsed. If an API sends such ids, ask for them as strings or parse them with a library that supports big integers.
Types disappear at runtime: validate the data
A TypeScript interface is erased when your code is compiled, so it cannot check what an API actually returns. If the server changes a field, the compiler will not notice and the error will show up later as a confusing crash. For data that crosses a trust boundary, validate it at runtime. The Zod output of this tool generates a schema that both validates the data and gives you the static type with z.infer, so one definition covers both.
The Schema tab can also validate your JSON against a JSON Schema, which is useful for checking sample responses against a contract before you write any code.
Naming and structure
Nested objects become their own interfaces, named after the key that holds them, with the singular form for arrays (a users array of objects gives User). When two different shapes would get the same name, a number is added so that nothing is overwritten, and identical shapes are reused. Change the root name in the field above the output, then rename the rest in your editor.
What is JSON?
JSON (JavaScript Object Notation) is a lightweight, text-based data format that is easy for people to read and for machines to parse. It is the de facto standard for web APIs, configuration files and data exchange between services. A JSON document is built from just six value types: objects, arrays, strings, numbers, booleans and null.
{
"name": "Ada",
"age": 36,
"admin": true,
"skills": ["math", "code"],
"address": { "city": "London" },
"manager": null
}JSON is stricter than JavaScript object literals: keys and strings must use double quotes, trailing commas are not allowed and comments are not part of the specification (RFC 8259).
Why format JSON?
APIs usually return minified JSON on a single line, which is efficient for machines but unreadable for people. Formatting (also called pretty printing or beautifying) adds consistent indentation and line breaks so that you can scan the structure, spot mistakes and review changes. Minifying does the opposite and strips whitespace to reduce size.
Frequently asked questions
How are optional fields detected?
When a key appears in some objects of an array but not in others, it is emitted as optional (key?: type). Fields whose values differ in type become union types.
Should I use interface or type?
The generator emits interfaces for objects, which are easy to extend and give clearer error messages. Rename or convert them freely.
Is my JSON uploaded to a server?
No. Parsing, validation, formatting, diffing and code generation all run inside your browser, using a Web Worker. Your data is never sent to any server, which makes the tool safe for API responses, configuration files and other sensitive payloads.
Does it work offline?
Yes, after the first visit. The editor and tools are stored in your browser, so you can use them without a connection. Only the TypeScript, CSS and HTML language workers used for extra highlighting in the Code tab are downloaded the first time you use them.
How large a JSON file can I open?
Files of 20 MB and more work. Parsing happens off the main thread and the tree view is virtualized, so only the rows you can see are rendered. The editor switches to a lighter mode for very large documents.
What is the difference between formatting and minifying JSON?
Formatting (pretty printing) adds line breaks and indentation so people can read the data. Minifying removes all optional whitespace to make the payload as small as possible for network transfer or storage. Both produce identical data.
Fix common JSON errors
- Unexpected token '<' in JSON at position 0
- Unexpected end of JSON input
- Expected property name or '}' in JSON
- Trailing comma in JSON
- Expected ',' or '}' after property value in JSON
- "undefined" is not valid JSON
- Bad control character in string literal in JSON
- Unexpected non-whitespace character after JSON
- Unexpected token 'N' or an invisible character in JSON
JSON examples to try
- Simple JSON Example
- JSON Array Example
- Nested JSON Example
- User Profile JSON Example
- Paginated API Response JSON Example
- Product Catalog JSON Example
- Order and Invoice JSON Example
- Blog Posts JSON Example
- package.json Example
- GeoJSON Example
- Application Config JSON Example
- Weather API JSON Example
Cheat sheets
More JSON tools
- JSON Viewer Online
- JSON Validator
- JSON to Go Struct
- JSON to Python (Pydantic)
- JSON Diff
- JSON Minifier
- JSON Pretty Print
- JSON to YAML
- JSON to CSV
- JSON to XML
- Large JSON Viewer
- JSON Tree Viewer
- JSON File Viewer
- JSON Sorter
- JSON to Zod
- JSON to Rust
- JSON to Java
- JSON Schema Validator
- JSON to JSON Schema
- JSONPath Tester
- JSON Path Finder