Skip to tool
All tools

JSON → Types

TypeScript, Go or Python types from a JSON sample.

Questions

What does this tool do?

JSON → Types reads a JSON sample and writes the types for it: TypeScript interfaces, Go structs with json tags, or Python dataclasses. Nested objects get their own type named after their key; arrays are merged item by item, so a field missing in some items becomes optional and a field that changes type becomes a union; null is noted; equal shapes share one type.

How are the types inferred?

From what the sample shows. A field present in every item of an array is required; a field missing from some is optional; a field whose type differs between items is a union; a value that is null in some items and set in others is nullable. It is one sample, not a schema: a field that happens to be filled everywhere in your sample comes out required, and a type the sample never shows is never written.

How are the types named?

The root gets the name in the Root name field; each nested object is named after its key in PascalCase — address becomes Address, an item of items becomes Item. Two objects with the same shape share one type, named for the first; two different shapes under the same key get a number: User, User2.

What does each language get?

TypeScript: interfaces, optional fields with ?, unions with |, unknown where the sample gives nothing. Go: structs with json tags, omitempty on optional fields, a pointer for a nullable field and interface{} for a union. Python: dataclasses with Optional[...] = None for optional and nullable fields, Union[...] for unions, Any where the sample gives nothing.

What about null?

A null value alone is typed null (TypeScript), interface{} (Go) or None (Python). Where an array's items have a value in some and null in others, the field is T | null, *T, or Optional[T].

Integers or floats?

Whole numbers are int in Go and Python, decimals float64 and float; TypeScript has only number. A field that is a whole number in one item and a decimal in another is a number in all three.

Related tools