Format Code Now
/
ConvertJSON to TypeScript Next: JSON Schema Generator ›

JSON to TypeScript Interface Generator

CV-15

Paste a JSON sample and press Convert to get TypeScript interfaces. Keys missing from some array items become optional, null becomes a union, and nested objects get their own interface.

Runs in your browser. Nothing is uploaded.

JSON

TypeScript

How to use it

  1. Paste a JSON sample into the input box, ideally one with several array items so optional keys show up.
  2. Choose the root type name, such as Root or Response.
  3. Pick interface or type, and turn export and optional keys on or off.
  4. Press Convert and copy the result into your .ts file.
  5. Check the types against the real API, because a sample only shows what it contains.

How types are inferred

Every object becomes an interface named after its key, written in PascalCase, and arrays use the key without its trailing s, so orders gives Order. When objects in an array have different keys, a key missing from some of them is marked optional with ?. A null value gives | null, and values of different types give a union such as (number | string)[]. An empty array becomes unknown[]. Every number is typed number.

Large integers and other limits

JavaScript numbers are accurate only up to 9007199254740991. An integer beyond that is typed number, and a comment warns you that JSON.parse will round it, so such IDs are safer as strings. Dates stay string, since JSON has no date type. The tool reads a sample, not a schema, so a field that is always present in your sample but optional in the real data will not be marked optional. It does not generate runtime checks.

Names and keys

Keys that are not valid identifiers, such as first-name, are quoted in the output. If two interfaces would get the same name, the second gets a number. Names that would hide a built-in type, such as Date, get Type added. Choose type instead of interface if you prefer type aliases, and turn off export if you are pasting the result into a file that exports elsewhere.

Error messages and what to do

MessageWhat it meansFix
Expected a string keyThe JSON has an object key that is not in double quotes, or a comma before a closing brace.Fix the JSON first. The Validate action of the JSON Validator shows the same error with a hint.
Unexpected end of inputThe JSON ended while a value or bracket was still open.Add the missing closing brace or bracket.

Questions people ask

How do I convert JSON to a TypeScript interface?

Paste a JSON sample into the input box and press Convert. You get an interface for the root object and one for each nested object. Copy the result into your project and rename the types if needed.

How are optional properties decided?

A key is optional when it is missing from at least one object that was merged with others, for example some items of an array. In a single object every key is present, so none are optional. Use a sample with several items.

How does it handle null values?

A null becomes | null. If the same key holds a string in one item and null in another, the type is string | null. If it is null in every item, the type is just null, so give it a sample with a real value.

Why are my large IDs typed as number?

JSON has one number type, and TypeScript maps it to number. An integer beyond 9007199254740991 is flagged with a comment, because JavaScript rounds it when parsing. Consider sending such IDs as strings.

Should I use interface or type?

Both describe the same shape here. Interfaces can be extended and merged, while type aliases can also name unions. The converter writes interfaces by default, and the Declare with option switches to type.

Does it create types for a whole API?

No. It reads one JSON sample and describes that sample. It does not know optional fields that the sample lacks, enums or value limits. Treat the output as a starting point and check it against the API's documentation.

About this tool

Infers types from the values you paste, so the result is only as complete as the sample. A key that is missing from some objects in an array becomes optional (?), a null becomes | null, and mixed types become a union. Every number is typed number; an integer beyond 9007199254740991 gets a comment, because JSON.parse in JavaScript rounds it. An empty array becomes unknown[]. Nested interfaces are named after their key, and arrays use the key without its trailing s. Dates stay string. It does not create runtime checks.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.