YAML Null Values
A null value means nothing is there. A form field left empty, a phone number nobody supplied, and an optional setting without a choice all represent null. YAML offers several ways to write it, and each behaves slightly differently from an empty string or a zero.
Ways to Write Null
middle_name: null
nickname: ~
spouse:
license: Null
fax: NULLAll five lines produce null. The tilde character is a shorthand. A key followed by nothing also produces null, which is the most common form in real files.
Null, Empty, Zero, and Missing
People often confuse four different ideas. A table separates them clearly.
| Idea | YAML | Meaning |
|---|---|---|
| Null | score: null | The key exists with no value |
| Empty string | score: "" | The key holds text with zero characters |
| Zero | score: 0 | The key holds the number zero |
| Missing | (no line) | The key does not exist at all |
A Layman's Picture
Picture four mailboxes outside a building:
Mailbox A Mailbox B Mailbox C Mailbox D [ empty ] [ blank page ] [ page: 0 ] (no mailbox) null empty string zero missing key
Mailbox A exists and holds nothing. A blank sheet sits in mailbox B. The number zero is written on a sheet in mailbox C. Nobody ever built mailbox D. A program treats each case differently.
Null in Lists
List items can also be null. A dash with nothing after it creates a null entry.
answers:
- yes
-
- no
- ~The list holds four entries. The second and fourth entries are null. Surveys use this pattern when a respondent skips a question.
How Programs See Null
| Language | Result |
|---|---|
| Python | None |
| JavaScript | null |
| Java | null |
| Ruby | nil |
Code that reads the value must handle this case. Asking for the length of a null name or adding one to a null count raises errors in most languages.
Null as an Optional Setting
Configuration files use null to say that a setting has no value yet.
database:
host: localhost
password: ~
timeout:The application can apply a default when it sees null. A developer reading the file understands that the key exists on purpose and awaits a decision.
Writing the Word Null as Text
A person named Null or a status labelled null needs quotes to stay text:
surname: "Null"
status: "null"Quotes tell the parser to read the characters literally. The same rule applies to the tilde symbol when it must remain a plain character.
Checking Null Safely in Python
import yaml
data = yaml.safe_load("nickname: ~\nage: 0")
print(data["nickname"] is None)
print(data["age"] is None)Null in Real Projects
Databases export missing fields as null. Survey tools record skipped questions as null. Application settings use null to mark options that await a decision. Recognizing the difference between an unknown answer and a zero answer keeps reports honest, because an average calculated over zeros differs from an average calculated over real answers.
The first line prints True and the second prints False. Zero is a real value, and only null counts as nothing. Using a plain truth test such as if not value mixes up null, zero, and empty strings, so compare against None directly.
Guidelines
- Choose null when a value is unknown or not applicable.
- An empty string fits text that exists but has no characters.
- Remove the key entirely when the setting does not apply and the program supports missing keys.
- Write the word null explicitly when readers might wonder whether the empty value was an accident.
