Represent null in JSON

How null works in JSON: the lowercase literal, how it differs from "null", empty strings and missing keys, how serializers and formatters treat it, and when to choose plain text instead.

JSON has one way to say "no value here": the bare literal null. It looks simple, but it is easy to confuse with a quoted string, an empty string or a missing key, and different languages convert their own empty values into JSON in different ways. This guide covers what null means in JSON, what formatting and validation tools do to it, and when it is the right choice.

Short answer: when null is the right choice

Use null when a field exists in your data model but currently has no value, and when the consumer needs to tell "known to be empty" apart from "not sent". Its main benefit is that every conforming JSON parser understands it. The tradeoff is that null, an omitted key and an empty string mean different things to the receiving code, so you have to pick one convention per field and document it. If the audience is a person rather than a program, such as a report, an email or a user interface, a phrase like "not provided" is usually clearer than a literal null.

What null is in JSON

RFC 8259, the JSON standard, defines three literal names: true, false and null. They must be lowercase, and no other literal names are allowed. A null value has no quotation marks around it. It can appear as an object member value, as an array element, or as an entire JSON document by itself.

{
  "name": "Ada",
  "middleName": null,
  "tags": ["admin", null, "editor"]
}

In this example, middleName is present and explicitly empty, and the second element of tags is an empty slot. Both are valid JSON.

null compared with "null", empty strings and missing keys

Form | Example | JSON type | Typical meaning
null literal | "middleName": null | null | Field exists, value is absent or unknown
Quoted text | "middleName": "null" | string | The four letters n-u-l-l, which is usually a bug
Empty string | "middleName": "" | string | A value that is known to be empty text
Omitted key | (no middleName member) | none | Not sent, not applicable, or unchanged, depending on the API

The distinction matters most in update requests. Many APIs read an omitted key as "leave this field unchanged" and an explicit null as "clear this field". That is a convention of the specific API, not a rule of JSON, so check the documentation of the service you are calling.

Spellings that are not valid JSON

  • NULL or Null: wrong case, rejected by strict parsers
  • None: Python's name for the value, not a JSON token
  • nil: Ruby and Go terminology, not a JSON token
  • undefined: a JavaScript value with no JSON equivalent
  • An empty position, as in [1, , 3]: not allowed in JSON arrays

Some lenient parsers accept a few of these spellings. A validator that follows RFC 8259 should flag every one of them. If a payload works in one tool and fails in another, a non-standard null spelling is a likely cause.

How languages turn their empty values into JSON

Serializers decide how native values map to null, and they do not all decide the same way. In JavaScript, JSON.stringify drops object properties whose value is undefined, replaces undefined inside arrays with null, and writes NaN and Infinity as null:

JSON.stringify({ a: undefined, b: null, c: NaN, d: [undefined] })
// '{"b":null,"c":null,"d":[null]}'

Python's json module writes None as null and reads null back as None. By default it also writes NaN and Infinity as the bare tokens NaN and Infinity. Those tokens are not valid JSON, so strict parsers in other languages may reject the output. Passing allow_nan=False makes the module raise an error instead.

import json
json.dumps({"a": None, "b": float("nan")})
# '{"a": null, "b": NaN}'

In practice, a null in received JSON may have started as a missing value, an undefined property or a non-numeric number. If that difference matters to you, encode it explicitly, for example with a separate status field, rather than relying on null.

What formatting and validation tools change

Pretty-printing and minifying change only the whitespace between tokens. A correct formatter leaves null, its position and its meaning untouched. A validator does not change the document; it reports whether tokens such as null are spelled and placed correctly.

Some tools do more than reformat. Options such as "remove empty values", key sorting, or conversion to another format can delete or rewrite nulls. YAML, for example, has several spellings for null, and CSV has no native null at all. No specific tool was tested for this article, so compare a tool's output with its input before relying on it for null handling.

In the JSON context, words like "reverse" or "mirror" have no safe meaning. Reversing the characters of a JSON document produces invalid JSON, and stylized Unicode "fonts" replace ordinary letters with different code points, so a styled null is no longer the null literal. Keep JSON in plain ASCII tokens and leave any visual styling to the display layer.

Declaring nullable fields in schemas

JSON Schema treats null as its own type. To allow a string or null, list both types:

{
  "type": "object",
  "properties": {
    "middleName": { "type": ["string", "null"] }
  },
  "required": ["middleName"]
}

With middleName in the required list, the key must be present but may be null, which separates "explicitly empty" from "missing". OpenAPI 3.0 expresses the same idea with nullable: true. OpenAPI 3.1 follows JSON Schema and uses the type array instead.

Will it survive copying and sharing?

The null literal is plain ASCII, so copying usually leaves it intact. The surrounding text is where problems tend to start:

  • Word processors and some chat apps convert straight quotes to curly quotes, which breaks every string around a null.
  • Rich-text editors may insert non-breaking spaces. JSON allows only space, tab, line feed and carriage return as whitespace.
  • Auto-capitalization can change null to Null at the start of a line.
  • RFC 8259 says JSON generators must not add a byte order mark, though parsers may ignore one. Files saved by some editors may still start with one.

Sharing JSON as a file, or in a code block that preserves plain text, avoids most of these issues. Running a validator after pasting confirms the document still parses.

Pretty-printed JSON is easier to read and edit than minified JSON, and it is easier to search for a specific null. Screen readers generally read null as an ordinary word, but this article includes no tests with specific assistive technologies or destinations. For people who cannot or will not read raw JSON, provide a plain-language version, for example a table where empty fields say "not provided".

Evidence gaps

This article relies on the JSON standard and on documented behavior of the JavaScript and Python standard libraries. It does not evaluate any particular online formatter, validator or editor, and it makes no claims about how a given tool stores or retains pasted data. Check those points against each tool's own documentation.