Master YAML: Essential Syntax, Data Types, and Advanced Features Explained
This article provides a comprehensive overview of YAML, covering its purpose, concise syntax, indentation rules, data structures, scalar types, built‑in tags, special types, and examples of JavaScript‑specific and custom types, making it a valuable reference for developers working with configuration files.
YAML is a concise and powerful language designed for writing configuration files.
YAML stands for "YAML Ain’t a Markup Language" and its design draws from JSON, XML, and SDL, emphasizing data‑centric, readable, and easy‑to‑write syntax.
YAML Syntax Features
People with programming experience find it very easy to understand.
Syntax Characteristics
Case‑sensitive
Uses indentation to represent hierarchy
Tab indentation is prohibited; only spaces are allowed
The number of spaces for indentation is not important as long as it is consistent within the same level
Comments are indicated with
# # yaml
languages:
- Ruby
- Perl
- Python
websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org
# Json
{
"languages": [
"Ruby",
"Perl",
"Python"
],
"websites": {
"YAML": "yaml.org",
"Ruby": "ruby-lang.org",
"Python": "python.org",
"Perl": "use.perl.org"
}
}Data Structures
Object: a dictionary of key‑value pairs
Array: an ordered list of items
Scalar: a single indivisible value
# scalar
hello
# array
- Cat
- Dog
- Goldfish
# object
animal: petsQuote Differences
Single quotes ( ''): treat special characters as literal strings
Double quotes ( ""): interpret special characters
# single quotes
name: 'Hi,
Tom'
# double quotes
name: "Hi,
Tom"Built‑in Types List
!!int: integer type !!float: floating‑point type !!bool: boolean type !!str: string type !!null: null value !!set: set collection !!seq: list (sequence) !!map: mapping (dictionary) !!binary: binary data !!timestamp: date‑time type !!omap / !!pairs: ordered map / key‑value list
YAML Scalars
Scalars are the most basic and indivisible values.
String
# without quotes
name: Tom
# using single quotes
name: 'Tom'
# using double quotes
name: "Tom"Boolean
debug: true
debug: falseNumber
# decimal integer
12 # ten‑base integer
014 # octal integer
0xC # hexadecimal integer
13.4 # floating‑point
1.2e+34 # exponent
.inf # infinityNull
# various null representations
date: ~
date: nullTimestamp
# ISO‑8601 format
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 only (00:00:00Z): 2002-12-14JavaScript Specific Types
# regular expression
simple: !!js/regexp foobar
modifiers: !!js/regexp /foobar/mi
# undefined
undefined: !!js/undefined ~
# function
function: !!js/function >
function foobar() {
return 'Wow! JS-YAML Rocks!';
}Custom Types
JS‑YAML allows defining custom YAML types for your structures. Example of a custom !sexy type:
foobar: !sexy
- bunny
- chocolateSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
