Skip to content

YAML Scalars

Learn about YAML scalar types (integers, floats, strings, booleans, timestamps, and special cases) with syntax examples and formatting rules.

YAML Scalars

YAML Scalar is a simple data type.

The value of the scalar can be

  • Numeric Data Types (integer, float, exponential, octal, hexadecimal, etc.)
  • String
  • Boolean
  • Timestamp (date and time)

Numeric Data Types

integer: 12

octal: 0o14

hexadecimal: 0xC

float: 13.4

exponential: 1.2e+34

infinity: .inf

negative infinity: -.inf

not a number .nan

String

YAML strings are Unicode.

Strings can be stored with or without quotes.

A string in YAML

'A single-quoted string in YAML'

"A double-quoted string in YAML"

The double-quoted style includes various escape sequences. Flow scalars can include multiple lines; line breaks are always folded in this structure.

For example, when using single-quoted strings, any single quote ' inside its contents must be doubled to escape it:

'A single quote '' inside a single-quoted string'

Strings containing any of the following characters must be quoted. Although you can use double quotes, for these characters it is more convenient to use single quotes, which avoids having to escape any backslash \:

:, {, }, [, ], ,, &, *, #, ?, |, -, <, >, =, !, %, @, `

Boolean

Booleans in YAML are expressed with true and false.

true-value: true # True, TRUE, y, Y, Yes
false-value: false # False, FALSE, n, N, No

Timestamp (Date and Time)

A timestamp value represents a single point in time.

YAML uses the ISO-8601 standard to express dates.

canonical:        2001-12-15T02:59:43.1Z
valid iso8601:    2001-12-14t21:59:43.10-05:00
space separated:  2001-12-14 21:59:43.10 -5
no time zone (Z): 2001-12-15 2:59:43.10
date (00:00:00Z): 2002-12-14

Tricky Cases

There are other cases when the strings must be quoted, no matter if you're using single or double quotes:

  • When the string is true or false (otherwise, it would be treated as a boolean value);
  • When the string is null or ~ (otherwise, it would be considered as a null value);
  • When the string looks like a number, such as integers (e.g. 3, 24, etc.), floats (e.g. 2.5, 24.5) and exponential numbers (e.g. 12e3, etc.) (otherwise, it would be treated as a numeric value);
  • When the string looks like a date (e.g. 2014-12-31) (otherwise it would be automatically converted into a Unix timestamp).

Explicit the Data Type

In YAML, nodes are given a type depending on the application.

Explicit typing is denoted with a tag using the exclamation point (!) symbol.

For example:

---
data:
    start_date: !!str 2022-05-14

    price: !!float 3

    picture: !!binary |
        R0lGODlhDAAMAIQAAP//9/X
        17unp5WZmZgAAAOfn515eXv
        Pz7Y6OjuDg4J+fn5OTk6enp
        56enmleECcgggoBADs=
Navigation