JSON to Code

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.

By · Updated 2026-11-06 · 6 minute read

JSON gives you three ways to say "there is nothing here", and they mean different things:

{
  "a": null,
  "b": "",
  "c": []
}

And a fourth, which is the key that is not there at all. A generator has to turn each of these into a type, and the choice it makes decides what your code has to handle.

The three cases, and what they usually mean

In the JSON What it usually means
The key is missing This field does not apply, or the API did not send it
The key holds null The field applies, and the value is unknown or cleared
The key holds "" or [] The field applies, and the value is empty

"Usually" is doing a lot of work in that table. An API decides its own meaning, and many APIs are not consistent with themselves. The important thing is that your type keeps the difference, so you can decide later.

What a generator does with a lone null

This is the trap. Give a generator one sample where a field is null:

{ "id": 7, "closed_at": null, "owner": null }

TypeScript gets this:

export interface Root {
  id: number
  closed_at: null
  owner: null
}

The type says the field can only be null. The generator is not being stupid; null is the only value it ever saw. But the code you get is useless: assign a date to closed_at and the compiler refuses.

Go does something different and equally awkward: with nothing to go on it writes interface{}, which means "anything", and you are back to casting by hand.

The fix: show it both ways

Give a second sample where the field holds a real value:

[
  { "id": 7, "closed_at": null },
  { "id": 8, "closed_at": "2026-02-01T10:00:00Z" }
]

Now TypeScript writes closed_at: Date | null, Rust writes Option<DateTime<Utc>>, and Swift writes Date?. Each one says the truth: there may be a date, and there may be nothing.

Missing and null are not the same

Some languages can tell these apart and some cannot.

Ask yourself whether your program cares. For a read, usually not: missing and null both mean "no value". For a write, it matters a great deal. In a PATCH request, a missing key normally means "leave this alone" and a null key means "clear this". Send the wrong one and you erase data.

The empty string is not null

{ "middle_name": "" }

This is a string. Its type is string. A generator will never make it optional, and it should not. But in your head, "" and null are the same thing most of the time, and that is where a bug grows: you check if (name) in JavaScript and an empty string fails the check exactly as null does, while in Python if name fails for "", None, 0 and [] alike.

If you control the API, pick one way to say "nothing" and keep to it. If you do not, write the check out in full and say what you mean.

What to do

  1. Never generate types from a single sample that has nulls in it. Find a sample with real values, or add one by hand.
  2. If a field is null in every sample you have, decide its type yourself and write it in. The generator cannot help you.
  3. For an API you own, say in the schema which fields are nullable. "type": ["string", "null"] is the way, and it survives into every language.
  4. Keep "missing" and "null" apart in any code that writes data back.

Try it

Open the two-record sample above in the tool, switch between TypeScript, Rust and Go, and watch how differently each language handles the same uncertainty. Then delete the second record and see all three get it wrong together.

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 Code

Read next

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.

snake_case to camelCase — what a generator renames, and why

Your JSON keys and your language's naming rules disagree. Here is what gets renamed, what keeps the original name, and where it goes wrong.