# JSON to TypeScript

> JSON to TypeScript converter: generate interfaces or type aliases from sample JSON, with nested objects, arrays, optional fields and null handled. Free, runs in your browser.

Source: https://widgetpantry.com/en/json-to-typescript · Türkçe: https://widgetpantry.com/tr/json-typescript-cevir

Paste an API response or any JSON sample: nested objects become named interfaces and arrays get proper types. Fields missing from some items are marked optional (?). Your JSON never leaves your browser.

Runs in your browser: files and text are not uploaded. [Use the tool](https://widgetpantry.com/en/json-to-typescript)

## How to use it

1. Paste a JSON sample on the left.
2. Choose the root name and interface or type style.
3. Copy the TypeScript and add it to your project.

## How JSON becomes types

The tool looks at the type of every value: text becomes string, numbers number, true/false boolean and null null. Each nested object becomes its own interface named after its key (address → Address), and arrays of objects get a singular name (users → User).

All objects in an array are merged into one type: keys missing from some items become optional (?), and values of different types become a union (string | number). Objects with the same shape share one interface. An empty array becomes unknown[], because the sample does not show what goes in it.

The types only reflect the sample you give: fields or values that never appear in it cannot be in the type. For a better result, use a response with several different items and review the output.

## Frequently asked questions

### Should I pick interface or type?

For objects both do the same job. Interfaces can be merged and read better in error messages; type aliases are more flexible with unions and utility types. Follow your project's convention.

### Why is a field optional (?)?

Some objects in the array did not have it. With "Make null fields optional" on, fields that hold null are written with ? too.

### Why are dates strings?

JSON has no date type, so dates arrive as text. You can change the type to Date or a branded type by hand.

### Is my JSON sent anywhere?

No. The conversion runs entirely in your browser; nothing is uploaded or stored.

## Related tools

- [JSON Formatter](https://widgetpantry.com/en/json-formatter): Online JSON formatter and validator: pretty-print, validate and minify JSON. Line and column for errors, 2/4-space or tab indentation, one-click copy.
- [JWT Decoder](https://widgetpantry.com/en/jwt-decoder): JWT decoder: view a token's header and payload as JSON, read exp, iat and nbf as dates and see whether it has expired. The token is not sent anywhere.
- [Case Converter](https://widgetpantry.com/en/case-converter): Case converter: upper, lower, title and sentence case, plus camelCase, PascalCase, snake_case, kebab-case and CONSTANT_CASE for code. Free, runs in your browser.
