C.W.K.
Stream
Lesson 03 of 08 · published

Scalars — Strings, Numbers, Booleans, Null

~12 min · yaml, scalars, strings, norway

Level 0Plaintext
0 XP0/64 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

YAML auto-types your scalars; you can override

A bare value like port: 8000 reads as an integer. port: "8000" reads as a string. The parser guesses the type from the bare form. This convenience is also YAML's most famous footgun.

The five scalar styles

  • Bare (plain)name: Pippa. Parser guesses type.
  • Single-quotedname: 'Pippa'. Always a string. Backslashes are literal.
  • Double-quotedname: "Pippa". Always a string. Supports JSON-style escapes (\n, \t, \u00e9).
  • Literal block (|) — multi-line, preserves newlines.
  • Folded block (>) — multi-line, joins lines into one with spaces.

Booleans, the Norway problem

YAML 1.1 (still the default in many parsers, including Kubernetes pre-1.22) treats 22 unquoted strings as booleans: y, Y, yes, Yes, YES, n, N, no, No, NO, true, True, TRUE, false, False, FALSE, on, On, ON, off, Off, OFF. So country: NO reads as country: False. The fix: always quote string values that look like keywords. country: "NO" is a string.

Null

Four bare forms all parse as null: null, Null, NULL, ~. An empty value (key with nothing after the colon) is also null. To say the literal string 'null', quote it: 'null'.

The Norway Problem (real story): a config file listed countries with two-letter ISO codes. countries: [GB, IE, NO, DE]. The YAML 1.1 parser turned NO into False. Norway disappeared from the list, the booking system silently stopped sending mail to Norwegian addresses, and the bug took weeks to find. Quote anything that could be misread as a keyword.

Code

Five scalar styles·yaml
bare:    Pippa
single:  'Pippa'
double:  "Pippa"
literal: |
  This is line one.
  This is line two.
  Newlines preserved.
folded:  >
  This is one
  long sentence
  joined by spaces.
Type guessing — bare values·yaml
an_int:    42
a_float:   3.14
a_neg:     -17
sci:       6.022e23
a_bool:    true
a_null:    null
an_empty:                # also null
empty_str: ""            # explicit empty string
The Norway Problem·yaml
# DANGEROUS — these all parse as booleans in YAML 1.1
country_a: NO        # → False
country_b: ON        # → True
flag:      yes       # → True

# SAFE — quote to force string
country_a: "NO"
country_b: "ON"
flag:      "yes"
The 1.2 fix (if your parser supports it)·yaml
%YAML 1.2
---
# Under YAML 1.2, only true/false/True/False/TRUE/FALSE are booleans.
# 'yes', 'no', 'on', 'off' are now plain strings.
# Most modern parsers default to 1.2; older ones still hit 1.1 rules.

External links

Exercise

Write a YAML file with a list of two-letter country codes including NO, ON, OFF. Parse it with PyYAML (yaml.safe_load) — note Norway and friends become booleans. Now wrap each in single quotes and re-parse. The diff is the Norway Problem in 30 seconds.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.