Skip to content
Scalars
step 1/5

Reading — step 1 of 5

Read

~1 min readYAML Basics

Scalars

A scalar is a single value: string, number, boolean, null.

string: hello
number: 42
float: 3.14
boolean: true
null_value: null

Type inference (YAML's footgun):

  • true, True, TRUE, yes, Yes, on, On → boolean true (YAML 1.1).
  • false, no, off → boolean false.
  • null, Null, NULL, ~, empty → null.
  • 42 → integer.
  • 3.14 → float.
  • 0xff → 255 (hex).
  • 0o17 → 15 (octal in YAML 1.2).
  • 2024-01-01 → date.
  • Anything else → string.

YAML 1.1's "Norway problem": country code NO → boolean false. Caused real bugs.

YAML 1.2 (preferred): only true, false, null are special. Others are strings.

To force string: quote it.

string: hello       # string
quoted: "true"      # string "true"
real_bool: true     # boolean
zip: "01234"        # string (without quotes, leading 0 → octal in 1.1)

Quoting:

  • Single quotes: '...'. No escapes; '' for embedded '.
  • Double quotes: "...". JSON-style escapes (\n, \", \u00ff).
escaped: "first\nsecond"
literal: 'first\nsecond'    # backslash-n LITERAL, no newline

Multi-line strings:

  • Literal block (|): preserve newlines.
  • Folded block (>): join lines with spaces; blank lines stay.
literal: |
  line 1
  line 2
  line 3

folded: >
  these lines
  will be joined
  with spaces.

  Blank line stays.

Result:

  • literal: "line 1\nline 2\nline 3\n".
  • folded: "these lines will be joined with spaces.\nBlank line stays.\n".

Modifiers:

  • |+ keep trailing newlines.
  • |- strip trailing newlines.
  • >- strip + fold.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…