JSON Null Versus an Absent Member

An object can contain a name whose value is null, or omit the name entirely. Those records can carry different application meanings.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Read a patch example
  2. Test visible consequences
  3. Trace a patch against an existing value
  4. Check membership before reading a nullable field

Read a patch example

In a nickname edit protocol, {"nickname":null} may clear the nickname and {} leave it as it was. The JSON syntax itself permits both and supplies no edit semantics. Optional record fields define those application choices.

Test visible consequences

Start with an existing nickname. Apply each message separately and compare the stored result. If both clear it, a lookup may have collapsed absence into null. A sentinel can preserve the distinction.[1]

Trace a patch against an existing value

Suppose a profile stores nickname River. An empty patch leaves it unchanged, while {"nickname": null} can clear it if the patch contract says so. The string "null" remains text and may be an ordinary nickname. This is an application decision on top of JSON syntax. See optional record fields.

Existing nickname "River"
{} → preserve "River"
{"nickname":null} → clear under the stated contract
{"nickname":"null"} → use string "null"

Check membership before reading a nullable field

An ordinary map lookup with a default null can collapse missing and explicitly null. Test both with a populated prior record so their outcomes differ. Specify whether unknown fields should be rejected or safely ignored.

References

  1. IETF, RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format ↩