Dev · 108 tools · nothing uploads

JSON to TypeScript

JSON to TypeScript. Infers types from an example JSON document. Nested objects become named types by their key; mixed-type arrays fall back to a broad/any type. Runs on your device.

Input

live

JSON to TypeScript reads one example document and prints an interface for every object in it. The outermost interface is called Root, nested ones take the name of the key above them, and a key that is not a valid identifier is quoted. Two objects sharing a key name give two interfaces with one name.

Questions

What does the output look like?

Plain interface declarations, one per object found in your document. The top-level type is called Root and nested objects are named after the key that holds them, capitalised. Types are printed depth first with the root last, so nested interfaces are declared before the ones that reference them.

How do JSON types map to TypeScript?

Strings become string, booleans become boolean, and every number becomes number, whole or not. An array becomes T[] where T comes from its first element. A null becomes the literal type null, and an empty array becomes unknown[], since there is no element to learn from.

Are any fields marked optional?

No. Every key in the example becomes a required property with no question mark, because the tool sees one document and cannot tell which fields might be absent. Add the question marks yourself afterwards, along with any union types for fields that hold more than one shape. Generating from an example that shows the minimum shape gets you closer.

What happens with mixed or ragged arrays?

Only the first element is inspected. An array whose entries have different shapes is typed from entry zero, so later entries might not satisfy the generated interface. Reorder the example so the fullest object comes first, or widen the type by hand.

Why did it refuse my file?

If the document has no object anywhere at the top level, there is nothing to name a type after and the tool stops with "top-level value has no object fields to name a type after". A JSON file holding a bare string, a number or an array of numbers hits this. Wrap the value in an object and run it again.

Can two generated interfaces end up with the same name?

Yes. Names come from the key, so two objects that live under the same key name in different parts of the document produce two interfaces with the same name and different bodies. Rename one before you paste the result into a project.

Related Dev tools

n→nJSON format / validateParse, validate and pretty-print JSON, with a clear parse-error message if it is broken.Runs on each file you dropn→nJSON sort / minify / escapeSort object keys, minify to one line, or escape/unescape JSON for embedding inside another string.Runs on each file you dropn→nJSONPath queryQuery JSON with a JSONPath expression: dot/bracket access, wildcards (*), recursive descent (..). No filter expressions (?()) or slices; those need a full JSONPath engine this build does not have.Runs on each file you dropn→nJSON Schema generatorInfer a draft-07 JSON Schema from an example JSON document. Every observed key is marked required, arrays are typed to accept every observed element.Runs on each file you dropn→nJSON to GoJSON to Go. Infers types from an example JSON document. Nested objects become named types by their key; mixed-type arrays fall back to a broad/any type.Runs on each file you dropn→nJSON to RustJSON to Rust. Infers types from an example JSON document. Nested objects become named types by their key; mixed-type arrays fall back to a broad/any type.Runs on each file you dropn→nJSON ↔ CSVConvert a flat JSON array of objects to CSV and back. CSV has no type system: JSON->CSV renders numbers/booleans/null as text and CSV->JSON always comes back as strings. A nested object or array field is refused by name rather than silently flattened. CSV parsing is fully quote-aware, embedded newlines included; a file with an unterminated quote is refused rather than guessed at.Runs on each file you dropn→n.env ↔ JSONConvert a .env file to a flat JSON object and back. Values are treated as strings both ways. No nesting, no type coercion of numbers/booleans on the JSON->env direction beyond JSON.stringify.Runs on each file you drop