Syntax and Structure 🧩¶
YAML (YAML Ain't Markup Language) is designed to be a human-readable data serialization format. Its syntax and structure are simple, making it easy to write and read.
Indentation and Whitespace 📏¶
Spaces Only: YAML uses spaces for indentation, not tabs. Consistent indentation is crucial.
Levels of Indentation: Indentation levels define the hierarchy and structure of the document.
yml
key1: value1
key2:
nestedKey1: nestedValue1
nestedKey2: nestedValue2
key2
contains a nested mapping, which is indented.
Comments 💬¶
Single-Line Comments: Start with #
. Used to add notes or explanations.
yml
# This is a comment
name: chaitu-ycr # Inline comment
Scalars 📜¶
- Basic Values: Strings, numbers, booleans, and nulls.
- Strings
- Plain:
name: chaitu-ycr
- Single-quoted:
name: 'chaitu-ycr'
- Double-quoted:
name: "chaitu-ycr"
- Multiline
- Block Scalar (
|
): Preserves line breaks. - Folded Scalar (
>
): Folds line breaks into spaces.
- Block Scalar (
- In YAML,
>+
,>-
,|+
, and|-
are used to control the formatting and handling of multiline strings, which are known as block scalars.
- Plain:
yml
block: |
This is a literal scalar.
Line breaks will be preserved.
Exactly as written.
block_keep: |+
This is a literal scalar.
Line breaks will be preserved.
Exactly as written.
block_strip: |-
This is a literal scalar.
Line breaks will be preserved.
Exactly as written.
folded: >
This is a folded scalar.
New lines will be folded into spaces
when rendered.
folded_keep: >+
This is a folded scalar.
New lines will be folded into spaces.
folded_strip: >-
This is a folded scalar.
New lines will be folded into spaces.
Lists (Sequences) 📋¶
Dash and Space: Use -
followed by a space for each item.
yml
fruits:
- 🍎 Apple
- 🍌 Banana
- 🍒 Cherry
Nested Lists: Lists can contain other lists or mappings.
yml
favorites:
- colors:
- 🔴 Red
- 🟢 Green
- 🔵 Blue
- numbers:
- 1️⃣ One
- 2️⃣ Two
- 3️⃣ Three
Mappings (Dictionaries) 📑¶
Key-Value Pairs: Separate keys and values with a colon and space.
yml
person:
name: chaitu-ycr
age: 30
city: chennai 🏙️
Nested Mappings: Mappings can contain other mappings.
yml
company:
name: ycr TechCorp
location:
city: chennai 🏙️
state: Tamil Nadu 🛕
Inline Collections 📖¶
Inline Lists and Mappings: Use square brackets for lists and curly braces for mappings.
yml
inline_list: [🍎 Apple, 🍌 Banana, 🍒 Cherry]
inline_mapping: {name: chaitu-ycr, age: 30, city: chennai 🏙️}
Anchors and Aliases 🔗¶
Reuse Values: Use anchors (&
) to define reusable content and aliases (*
) to reference it.
yml
defaults: &defaults
country: INDIA in
currency: RUPEE 💵
location1:
<<: *defaults
city: chennai 🏙️
location2:
<<: *defaults
city: Pondicherry 🌴
defaults
anchor is reused inlocation1
andlocation2
with<<:
*defaults
.
Complex Keys 🧩¶
Use Complex Types: Sequences or mappings can be used as keys by enclosing them in ?
and :
.
yml
? [first, second]
: "Tuple as a key"
Multi-Document Files 📂¶
Separate Documents: Use ---
to separate multiple documents within a single YAML file.
yml
---
document1:
key1: value1
---
document2:
key2: value2