From a sample, to a schema, to code
The way to use a generator that still works in six months: make the schema once, read it, commit it, and make the code from it every time.
Most people use a code generator once. They paste an API answer, copy the types into the project, and never open the tool again. Then the API changes, and the types are a lie that the compiler enforces.
There is a better shape for this, and it costs one extra step.
The three steps
1. Make a schema from your samples. Paste one or more real answers into the tool and pick JSON Schema as the language. You get a draft of the contract in a few seconds.
2. Read it, correct it, commit it. This is the step that matters, and it is the one that is skipped. A schema made from a sample is a guess. You are the only one who can turn it into a fact:
- Which fields are truly required? A sample marks everything required.
- Which may be null?
- Which strings are really a small set of values? Add an
enum. - Which numbers have limits? Add
minimumandmaximum. - What does each field mean? Add a
description. It ends up in the generated code as a comment, in every language.
Then commit user.schema.json next to the code it describes.
3. Make the code from the schema, in the build. Switch the tool to A JSON Schema as the input, and generate TypeScript, Go, Python — whatever your services use. In a real build, use the quicktype command line tool for this step so it runs with no hands.
Why the middle step pays
Because the schema becomes the one place the truth lives.
A type in TypeScript and a struct in Go both describe the same API. Today they agree, because you wrote them on the same afternoon. In four months somebody adds a field to the Go side and forgets the TypeScript side. Nothing breaks until it does.
With a schema in the middle, there is nothing to forget. Both files are generated from one document, in CI, every time. A change to the schema shows up as a change in both languages in the same pull request, where a reviewer can see it.
What the folder looks like
schemas/
user.schema.json committed, reviewed, the contract
order.schema.json
generated/ never edited by hand
user.ts
user.go
user.py
Two rules make it work:
- Nothing in
generated/is edited. Put a header in the file saying so. Every generator writes one for you. - The build makes it again and fails if it differs. See the guide on generated code in CI.
When the schema is not yours
If you call somebody else's API, you still want the schema, and you probably have to write it yourself. That is not wasted work: writing it forces you to decide what your program actually depends on. A field you did not put in the schema is a field you are not reading, and when the API drops it, nothing happens.
Some APIs publish an OpenAPI document. The schemas are inside it, under
components.schemas, and you can lift the one you need straight out.
When to skip all of this
A script. A one-off migration. A spike you will throw away on Friday. Paste, copy the types, move on. The ceremony is for code that has to live.
The honest cost
An extra file, a build step, and a rule your team has to keep. In exchange you get one description of your data instead of one per language, and a change that cannot be half applied.
Try it now
- Paste a real API answer into the tool.
- Pick JSON Schema and copy the result into a file.
- Open the file and fix the
requiredlist, which will be wrong. - Change the input to A JSON Schema, paste your corrected schema back in, and pick Go, then Python, then TypeScript.
Three languages, one source. That is the whole idea.
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to CodeRead next
Keep generated code honest — make it again in CI
Generated files rot the moment somebody edits one by hand. One CI step makes that impossible.
The API changed. Now what?
A new field, a removed field, a type that flipped. Which changes break your generated code, which break it quietly, and how to find out first.