Skip to main content

YAML Scalars

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)
note

Scalar content can be written in block notation by using the following symbols

  • |: All live breaks are significant.
  • >: Each line break is folded to space. It removes the leading whitespace for each line.
data: |
YAML
(YAML Ain't Markup Language)
is a data-serialization language
data: ?
YAML (YAML Ain't Markup Language)
is a data-serialization language

Numeric Data Types

# an integer
integer: 12

# an octal
octal: 0o14

# an hexadecimal
hexadecimal: 0xC

# a float
float: 13.4

# an exponential number
exponential: 1.2e+34

# infinity
infinity: .inf

# negative infinity
negative infinity: -.inf

# infinity
not a number .nan

String

YAML strings are Unicode.

Strings can be stored with or without quotes.

# without quotes
A string in YAML

# with single quotes
'A single-quoted string in YAML'

# with double quotes
"A double-quoted string in YAML"
note

Quoted styles are useful when a string starts or end with one or more relevant spaces, because unquoted strings are trimmed on both end when parsing their contents.

note

Quotes are required when the string contains special or reserved characters.

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:
# this value is parsed as a string (it's not transformed into a DateTime)
start_date: !!str 2022-05-14

# this value is parsed as a float number (it will be 3.0 instead of 3)
price: !!float 3

# this value is parsed as binary data encoded in base64
picture: !!binary |
R0lGODlhDAAMAIQAAP//9/X
17unp5WZmZgAAAOfn515eXv
Pz7Y6OjuDg4J+fn5OTk6enp
56enmleECcgggoBADs=