Which JSON fields are optional? What a generator can and cannot know
A sample shows you what was there once. It cannot show you what is always there. Here is how to tell the difference, and what to do about it.
You paste one API answer into a generator and get a type back. Every field in that type is required, because every field was there in your sample. Two weeks later the program crashes on an answer where one key is missing.
The generator did nothing wrong. It was told one thing and it believed it.
What a sample can tell you
A sample is a list of keys that were present, once. That is all. It cannot tell you:
- which keys are always present;
- which keys may be missing;
- which keys may hold null;
- which keys appear only for a certain kind of user, or only after an event.
A type made from one sample is a guess, and it is always the most hopeful guess: everything is there, and nothing is null.
Give it more than one sample
The simple fix is more evidence. Two answers that differ make the difference visible:
[
{ "id": 1, "name": "Ada", "nickname": "The Countess" },
{ "id": 2, "name": "Alan" }
]
Open this example in the tool →
With both of these in front of it, a generator marks nickname optional and
leaves id and name alone. That is the rule: a key missing from any
sample becomes optional.
Where do you get a second sample? From the places the data already is:
- the test fixtures in your repository;
- a log of real answers, with the private parts removed;
- the API documentation, which usually shows two or three shapes;
- the answer for an empty list, or for a new account, which is the one that differs most.
Null is a third case
A key may be present and hold null. That is not the same as missing, and most languages make you treat it differently:
{ "id": 7, "closed_at": null }
Open this example in the tool →
TypeScript writes closed_at: null from this, which is worse than useless: it
says the field can only ever be null. Give it a second sample where the field
holds a date and you get closed_at: Date | null, which is the truth.
Read null in JSON for the whole of this.
Start from a schema instead
A JSON Schema holds the answer in the document itself:
{
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"nickname": { "type": "string" }
},
"required": ["id", "name"]
}
Open this example in the tool →
required is a fact, not a guess. Change The sample is to A JSON
Schema in the tool and every language gets the same answer, because the
answer is in the schema and not in the sample.
This is why the best way to use a generator is in two steps. Make a schema from your samples one time. Read it, correct it, and commit it to your repository. Then make the code from the schema, in every language, in your build.
What each language does with an optional field
| Language | A required field | An optional field |
|---|---|---|
| TypeScript | name: string |
nickname?: string |
| Python | name: str |
nickname: Optional[str] = None |
| Go | Name string |
Nickname *string |
| Rust | name: String |
nickname: Option<String> |
| Swift | let name: String |
let nickname: String? |
| Java | String name |
String nickname — the same |
Two of these are worth a second look.
Go has no optional. The generator uses a pointer, and a pointer is the only way to tell "the key was missing" from "the key held the empty string". If you take the pointer out to make the code tidier, you throw that away.
Java cannot show it in the type at all. A field is a reference, and a reference can be null, so a required field and an optional one look the same. The check has to happen in your code.
The rule to work by
Decide what is required from what your API promises, not from what it sent you this morning. If no promise exists in writing, treat the field as optional and handle the missing case. The crash you prevent is your own.
Try it
Paste the two-record sample above into the tool and watch nickname become
optional. Then take the second record away and watch it become required again.
That is the whole lesson in two clicks.
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
null in JSON — missing, empty, or unknown?
A key that is absent, a key that holds null, and a key that holds an empty string are three different things. Most bugs here come from treating them as one.
The number that does not fit — int64, bigint and decimal
A JSON number above 9007199254740992 is already wrong by the time a generator sees it. Here is why, and which type to ask for instead.
Dates in JSON, and the type a generator should give you
JSON has no date. What you get depends on the format in your sample, and an epoch number is almost always missed.