StringMash.com

JSON to TypeScript

Types from a JSON sample, in TypeScript, Zod, Go or Python.

222 characters
Updates as you type

Show the steps
  1. 3 object types read from the sample; 1 field is missing from some items, so marked optional.
  2. Types come from this sample only: a field that is always null here, or an empty array, can't say what it holds, so check those by hand.

Using it

Paste a JSON value, such as an API response, and the types appear as you type. Write as picks TypeScript interfaces or type aliases, Zod schemas (with the TypeScript type inferred from each), Go structs with json tags, or Python dataclasses. Top-level name names the outermost type. The JSON stays in your browser, which matters when the sample is a real response with real data in it.

How the types are worked out

Each object becomes a named type, called after the key it sits under: an orders array holds Order. Arrays merge all their items, so a key that only some items have becomes optional (note?: string), and a key that's null in some items and a string in others becomes nullable. Whole numbers become integers in Zod, Go and Python, since those can tell the difference; TypeScript has one number type.

Types can only be as good as the sample. A field that's always null, or an empty array, says nothing about what it holds, so those come out as null or unknown[] and are worth checking by hand. Paste a few items with every field filled in for the best result.

Questions

Interface or type?

For object shapes they're interchangeable in most code. Interfaces can be extended and merged; type aliases can also name unions. Pick whichever your project already uses.

Why does Go use a pointer for some fields?

A *string can be nil, which is how Go tells a missing or null field apart from an empty one. Optional and nullable fields get one, with omitempty on the tag.

Is the JSON sent anywhere?

No. It's read and turned into types by code running on this page.

Sources

Added . What's new