BudiBadu Logo
Samplebadu

YAML by Example: Null Values

1.2

Representing absence of value. Three ways to express null.

Code

# Using null keyword
middle_name: null

# Using tilde
spouse: ~

# Empty value (no value after colon)
optional_field:

# All three are equivalent
config:
  value1: null
  value2: ~
  value3:

# Null is different from empty string
empty_string: ""
null_value: null

Explanation

YAML represents null values three ways: the keyword null, a tilde ~, or leaving the value empty. All three parse to the same null value in your programming language (like Python's None or JavaScript's null). The tilde is compact while null is more explicit.

Remember that null is different from an empty string. Null means no value exists, while an empty string is still a value. This distinction matters for optional configuration fields versus fields that should be explicitly blank.

Code Breakdown

2-6
Three equivalent ways to represent null values.
15
Empty quotes create an empty string, not null.