About the JSON to TypeScript Converter
Typing an API response by hand is slow and easy to get wrong, especially with deeply nested data. This JSON to TypeScript converter reads a sample of JSON and writes interfaces or type aliases for it: a root type, a named type for every nested object, typed arrays, union types where values vary and optional fields where some items lack a key. Review the result against the API documentation, since one sample cannot show every case.
Every value is mapped to its TypeScript type: strings to string, numbers to number, true and false to boolean, null to null and objects to a new interface named after their key in PascalCase, with plural keys made singular for array items (items becomes Item). Arrays of objects are merged into one interface: a property that appears in only some items becomes optional with ?, and a property with several value types becomes a union such as string | number. Keys that are not valid identifiers are quoted, and identical nested shapes share one interface.
Example: {"id": 1, "name": "Ann", "tags": ["a"], "address": {"city": "X"}, "items": [{"sku": "a", "qty": 1}, {"sku": "b"}]} produces a Root interface with id: number, name: string, tags: string[], address: Address and items: Item[], plus Address with city: string and Item with sku: string and qty?: number.
A sample can only show the fields it contains, so review the result against your API documentation. To tidy the JSON first use the JSON formatter, to sort keys the JSON sorter, and to escape strings the JSON escape tool.
How to use the JSON to TypeScript Converter
- Paste a JSON sampleAn object or an array, such as an API response.
- Name the root typeRoot by default, or a name such as User or ApiResponse.
- Choose the styleinterface or type, exported or not.
- Copy the TypeScriptPaste the definitions into a .ts file.
JSON to TypeScript Converter features
- Interfaces or type aliases
- Named nested interfaces
- Typed arrays and empty arrays
- Optional fields from arrays of objects
- Union types for mixed values
- Quoted keys when needed
- Shared interfaces for identical shapes
Common uses
- Typing REST API responses
- Config and fixture files
- Migrating JavaScript to TypeScript
- Front-end and Node projects
- Documenting data structures
JSON to TypeScript in practice: typing a tracking API, several samples, types versus validation, naming, IDs, status fields and OpenAPI
Generating TypeScript types from a JSON sample turns an unfamiliar API response into autocomplete, compile-time checks and documentation in seconds. The result is only as good as the sample and the review that follows, however. This guide types a courier tracking response, then covers using several samples, why types do not validate data, naming, identifiers, status fields, key styles and generating types from an API specification instead.
A worked example: typing a tracking response
A Next.js app shows parcel tracking from a courier API. A sample response contains a tracking number, a status and an events array, where most events include a location but the first does not. The converter produces a Root interface, renamed to TrackingResponse, and an Event interface in which location is optional because it is missing from one item. In the app, const data: TrackingResponse = await res.json(); now gives autocomplete for every field.
Use several samples
One response rarely shows every field. A parcel just booked has no events, a delivered parcel has extra fields such as a signature name, and an error response looks completely different. Placing several real responses together in one array and converting that array merges their fields, marking those missing from some samples as optional. The more varied the samples, the more accurate the types.
Types describe, they do not check
Annotating the result of res.json() tells TypeScript what you expect but checks nothing at runtime. If the API changes a field from number to string, the app compiles happily and fails later. Validating responses at the boundary, with a schema library or simple checks on critical fields, turns unexpected changes into clear errors instead of strange behaviour deep in the interface.
Naming
Rename generic names such as Root and Item to meaningful ones like TrackingResponse and TrackingEvent before using them across a codebase. Several files generated separately may each contain an Item interface describing different things. Keeping API types in a dedicated folder, one file per API area, makes them easy to find and update.
Identifiers
A sample may show an ID as a number, but some APIs send large IDs as strings to avoid precision loss in JavaScript, which handles whole numbers exactly only up to 9,007,199,254,740,991. Check the API documentation, and prefer string types for identifiers, since they are compared rather than calculated with.
Status fields
A status typed as string accepts any text, including typos. After checking the documentation, replacing it with a union of the allowed values, such as "booked" | "in_transit" | "delivered" | "returned", gives autocomplete and lets TypeScript warn when a switch statement forgets a case. Samples cannot reveal every possible value, so this step needs a human.
Key styles
APIs often use snake_case keys such as tracking_number, while TypeScript code usually prefers camelCase. Keep the generated types exactly as the API sends data. If you prefer camelCase in the app, convert the data in one mapping function and write separate types for the converted shape, rather than renaming keys in the API types, which would no longer match reality.
Generating types from an API specification
When an API publishes an OpenAPI specification, generating types from it is more reliable than working from samples, because the specification lists every field, optional property and allowed value. Tools exist to do this automatically. Sample-based conversion remains ideal for undocumented APIs, quick prototypes and third-party services without specifications.
Readonly data
Data received from an API is usually not meant to be changed in place. Marking properties readonly, or wrapping types with TypeScript's Readonly helper, makes the compiler flag accidental mutations, which helps in state management where changing an object directly can stop the interface updating.
Common mistakes
- Generating types from one sample and trusting them completely.
- Treating type annotations as runtime validation.
- Keeping vague names such as Root and Item.
- Renaming keys in types without converting the data.
Related tools
To explore a response before typing it, use JSON Viewer. To compare responses from two API versions, try JSON Diff, and to convert YAML configuration first, YAML to JSON Converter.
Keeping types up to date
APIs gain fields over time. Regenerating types from fresh samples every so often, and comparing the new file with the committed one, shows exactly what the API added or changed. Committing the types alongside tests that use them means a breaking change surfaces as a failing build rather than a bug report.
Sharing types across a project
In projects where both the server and the browser code use TypeScript, define the API types once in a shared folder or package and import them on both sides. Server changes that alter a response shape then cause compile errors in the front end immediately, before anything reaches users.
Privacy
Types are generated in your browser. Your JSON samples are not uploaded or stored, but remove personal data from real responses before sharing them anywhere.
Frequently asked questions
How do I convert JSON to a TypeScript interface?
Paste a JSON sample here and copy the generated interfaces. Each nested object gets its own interface, arrays become typed arrays and missing fields become optional.
How are optional fields detected?
From arrays of objects: if a property appears in some items but not others, it is marked optional with ?. A single object cannot show which fields are optional, so check your API documentation.
Should I use interface or type?
Both work for object shapes. Interfaces can be extended and merged, and many style guides prefer them for objects; type aliases are needed for unions and mapped types. Choose either here.
How are arrays with mixed types handled?
They become an array of a union, such as (string | number)[]. Arrays of objects are merged into one interface with optional and union fields. Nested arrays are typed the same way, giving types such as number[][].
What happens to null values?
A field that is null in the sample is typed null. If it can also hold another type, edit it to a union such as string | null, or include a sample where it has a value.
Why are some keys in quotes?
Keys that are not valid JavaScript identifiers, such as first-name or 2fa, must be quoted in TypeScript: "first-name": string. Quoted keys work the same way when you access them with bracket notation, such as user["first-name"].
Can I generate types for an array at the root?
Yes. If the JSON is an array, the root becomes a type alias for an array of the item interface, such as type Root = Item[].
Does it support enums or dates?
No. JSON has no date or enum type, so dates are typed as string. You can refine the generated types by hand, for example to a union of string literals.
Can I use the result with Zod or other validators?
The output is plain TypeScript types, which describe data only at compile time. For runtime validation, write a schema with a library such as Zod using these types as a guide.