Lesson 4: Scalars
A scalar is an atomic data type. This includes strings, numbers, boolean values true and false, and null.
The following example is a bare document with a single scalar value.
My single value document.
The following example demonstrates a map with various scalar types of values.
greeting: Hello favoriteNumber: 123 favoriteDessert: 3.14 favoriteChild: null areYouASentientBeing: true partyTime: 2021-12-31T23:59:59Z
Escaped characters
Escape means to remove special meaning from a character so it can be used literally.
Keys or values that contain the following characters must be escaped:
- starts with a
-
character followed by a space - has a
:
character followed by a space - has a
#
character preceded by a space - all non-printable characters.
Escape a single character with single quotes, or all characters in a scalar with double quotes. Another option that escapes is using blocks covered in the next lesson.
The following is an example of some problem unescaped values and their corresponding escaped solutions.
Problem | Solution |
---|---|
- plums | '- plums' |
Space: An Odyssey | 'Space: An Odyssey' |
The #3 YAML mistake | 'The #3 YAML mistake' |
Exercise 4.1: Spot the key with the problem value
Author: Szy de'Root bookTitle: Docs: investment in productivity publicationDate: 2021-12-31
See answer
The bookTitle
corresponding value must be escaped because it has a :
followed by a space.
The correct YAML is:
Author: Szy de'Root bookTitle: 'Docs: investment in productivity' publicationDate: 2021-12-31
It could also be escaped with double quotes:
Author: Szy de'Root bookTitle: "Docs: investment in productivity" publicationDate: 2021-12-31
Exercise 4.2: Spot the key with the problem value
Author: R2:D2 bookTitle: Lessons - From Observing Life chapterTitle: Lesson #1 publicationDate: 2021-12-31
See answer
The chapterTitle
corresponding value needs to be escaped because it has a #
preceded by a space.
The correct YAML is:
Author: R2:D2 bookTitle: Lessons - From Observing Life chapterTitle: 'Lesson #1' publicationDate: 2021-12-31
It could also be escaped with double quotes:
Author: R2:D2 bookTitle: Lessons - From Observing Life chapterTitle: "Lesson #1" publicationDate: 2021-12-31
Some characters didn't need to be escaped:
- the
:
inR2:D2
because it's not followed by a space - the
-
inLessons - From Observing Life
because the string doesn't start with-
. - the
-
in the2021-12-31
because the string doesn't start with-
.