Skip to content
SPCXTools

JSON to TypeScript

Instantly convert JSON data into TypeScript interfaces or types with customizable root names and export options.

Runs locally — files never leave your device

Loading tool…

How to use JSON to TypeScript

  1. 1Paste or type your JSON object or array into the input box. You can also click Sample to see how it works.
  2. 2Set the root name for your top-level object (defaults to "Root").
  3. 3Choose whether to generate a TypeScript interface or a type alias, and toggle the export keyword.
  4. 4Copy the generated TypeScript code to your clipboard or download it as a .ts file.

A fast, private JSON to TypeScript converter

When working with external APIs, complex configuration files, or document databases in a TypeScript project, manually writing out type definitions can be tedious and error-prone. This free online json to typescript converter instantly translates any valid JSON object or array into strongly typed TypeScript definitions.

Because the tool runs entirely locally in your web browser, your data is never uploaded to a server. You can safely paste proprietary API responses, internal database dumps, or sensitive configuration data without worrying about data privacy. The conversion happens instantly as you type or paste, allowing you to iterate quickly.

Generate TypeScript types from JSON flexibly

Every codebase has its own style guidelines and conventions. This tool offers several configuration options to ensure the output matches your project's requirements perfectly:

Custom Root Name: By default, the top-level object is named Root. You can easily change this to match your domain model, such as UserResponse, AppConfig, or ProductList.

Interface vs. Type: You can choose whether to generate a json to ts interface or a type alias. Interfaces are often preferred for object shapes in object-oriented patterns because they support declaration merging and can be extended. Types are highly versatile and are often preferred in functional programming patterns. The tool lets you switch between them with a single click.

Export Keyword: Toggle the export checkbox to automatically prepend the export keyword to your generated types, making them immediately ready to be imported into other modules in your application.

Optional Nulls: In many JSON APIs, a missing or undefined value is represented as null. In strict TypeScript environments, you often want to mark these fields as optional (using the ?: syntax) rather than strictly requiring a null type. Toggling the "Optional null" checkbox automatically handles this pattern, saving you from manually adding question marks to dozens of fields.

How the json to type inference works

When you provide a JSON payload, the converter deeply analyzes the values to infer the correct TypeScript primitives. Strings, numbers, and booleans are mapped directly to their TypeScript equivalents. Nested objects are recursively processed into nested interfaces or inline object types, keeping your data structures strictly typed all the way down.

For arrays, the tool examines the elements inside to determine the most accurate type. If an array contains uniform data, it generates a simple array type like string[] or number[]. If the array contains mixed data types, it intelligently creates a union type, such as (string | number)[]. If an array is completely empty, it defaults to any[] since there is no data available to infer a more specific type.

Common use cases

Consuming REST APIs: When integrating a new endpoint, you often receive a large, deeply nested JSON response. Pasting that response into the converter instantly gives you the exact interfaces needed to type your fetch or axios calls. This enables strict type-checking and rich autocomplete in your IDE, preventing runtime errors caused by typos in property names.

Migrating JavaScript projects: If you are incrementally migrating a legacy JavaScript codebase to TypeScript, you can use existing JSON configuration files or mock data payloads to quickly scaffold your data models without writing them from scratch.

Working with NoSQL databases: Document databases like MongoDB often return complex JSON structures. Generating types for these documents ensures your application logic handles the data safely and respects the schema of your database.

Tips for best results

  • Ensure strictly valid JSON: Unlike standard JavaScript objects, JSON requires double quotes around all keys and string values, and it does not allow trailing commas. If you have a messy payload or a JavaScript object literal, you can run it through the JSON Formatter first to validate and clean it up.
  • Provide a representative sample: The types generated are only as accurate as the data provided. If an API sometimes returns a string and sometimes returns a number for a specific field, make sure your sample JSON includes both in an array, or manually adjust the generated type to a union later. The richer your sample data, the more accurate your TypeScript definitions will be.
  • Convert from other formats: If your data comes from other formats, you might need to convert it first. For example, you can use the XML to JSON or YAML to JSON tools to get a valid JSON payload before generating your TypeScript definitions.

Frequently asked questions

How does the converter infer types?
It analyzes the actual values in your JSON data. Strings become string, numbers become number, booleans become boolean, and nested objects or arrays are recursively converted into nested types or interfaces.
What happens if my JSON has arrays with mixed types?
The tool will generate a union type (for example, (string | number)[]) that accurately reflects the different data types present in the array.
What does the "Optional null" setting do?
When enabled, any JSON property with a null value will be marked as an optional property in the TypeScript definition. This is especially helpful for handling sparse API responses where missing data is represented by null.
Can I generate a type alias instead of an interface?
Yes, you can toggle the style setting to "type" to generate type Root = { ... } instead of interface Root { ... }.
Is my JSON data sent to a server?
No. All parsing and conversion happen locally in your browser. Your data is never uploaded, making it completely safe for proprietary or sensitive API responses.
Why is my JSON causing an error?
The input must be strictly valid JSON. Common issues include missing quotes around keys, trailing commas, or using single quotes instead of double quotes.