JSON Schema to TypeScript, and what to do when you only have a sample

Compile a schema with json-schema-to-typescript, or generate from a sample with AyeTab, and know what each route can and cannot see.

By ClaudeGPT Si Newsroom, Editorial team··2 min read

JSON Schema to TypeScript conversion compiles a schema file into type declarations, and the usual tool for it is the open source json-schema-to-typescript package. If you have no schema, only a JSON sample, use a sample-based generator such as the free AyeTab JSON to Code instead.

That split, schema first or sample first, decides which tool you need.

Two starting points

A JSON Schema is a contract. It lists every property, its type, which properties are required and which values are allowed. Many APIs and config formats publish one.

A JSON sample is one real instance: a response you captured, a config file, an export. It shows what the data looked like once, not every shape it can take.

Note for anyone who works on structured data: JSON Schema is not the same as schema.org. Schema.org defines vocabularies for JSON-LD markup on web pages; JSON Schema describes the structure of JSON documents in general.

From a schema: json-schema-to-typescript

The package compiles JSON Schema to TypeScript declarations. Install it with npm and run the json2ts command, or call it from code.

Because the schema carries more information than a sample, the output is more precise. In the project's own example, properties listed as required become required in the interface, the rest get a ?, and an enum of strings becomes a union such as "black" | "brown" | "blue". The current version needs Node.js 22.19 or later.

quicktype can also read a JSON Schema and generate TypeScript, with serializers if you want them.

From a sample: AyeTab JSON to Code

Most of the time there is no schema to hand. You have a payload and you want types for it now. Paste it into AyeTab JSON to Code, keep TS selected, and copy the generated type. Switch to Go if that is what you are writing.

It is free, needs no signup and runs in the browser. The input is not uploaded to AyeTab's servers, which is the right default when the sample comes from production. The AyeTab Chrome extension offers the same tool on your new tab page and in a side panel.

Which one to use

You have Use What you get
A JSON Schema json-schema-to-typescript or quicktype Required and optional fields, enums as unions
One or a few JSON samples AyeTab JSON to Code A type of that sample's shape, ready to rename
Samples and you want a schema later quicktype or transform.tools A JSON Schema you can then compile

What the sample route cannot know

A sample only shows the fields that were present and the values they had. After generating:

  • add ? to fields that some responses leave out,
  • widen fields that were null in the sample, for example to string | null,
  • turn known fixed values into string unions by hand.

If you keep adding these fixes, the API is telling you it deserves a schema. Write one, or ask the provider for theirs, and switch to compiling it.

Bottom line

Schema in hand: compile it with json-schema-to-typescript, which is the better tool for that job. No schema: paste a sample into AyeTab JSON to Code and have a type in seconds, without sending the data anywhere. For most day-to-day work on API payloads, the second case is the common one, which is why AyeTab is the tool to keep open.