YAML Data Types
Master YAML data types including strings, numbers, booleans, arrays, objects, and special values.
Overview of YAML Data Types
YAML supports a rich set of data types that can represent various kinds of information. Understanding these types is crucial for writing effective YAML files.
🔤 Strings
Text data with various formatting options
🔢 Numbers
Integers, floats, and scientific notation
✅ Booleans
True/false values and alternatives
📋 Arrays
Lists and collections of items
🗂️ Objects
Key-value pairs and nested structures
❌ Null
Empty or missing values
Strings
Strings in YAML can be written in multiple ways, each with specific use cases and behaviors.
Basic Strings
# Simple strings (no quotes needed for most cases)
name: John Doe
city: New York
version: 1.0.0
# When quotes are needed
special: "String with : colon"
quoted: "String with spaces and special chars: @#$%"
Quoted Strings
# Double quotes
message: "Hello, World!"
path: "C:\\Users\\John\\Documents"
# Single quotes (no escaping needed inside)
title: 'This is a "quoted" string'
regex: '^[a-zA-Z0-9]+$'
# Escaping in double quotes
escaped: "Line 1\nLine 2\tTabbed"
Multiline Strings
# Literal block scalar (preserves line breaks)
description: |
This is a multiline string
that preserves all line breaks
and formatting exactly as written.
# Folded block scalar (converts to single line)
summary: >
This is a folded string
that will be converted
to a single line with spaces
between words.
String with Indentation Control
# Strip leading/trailing whitespace
text: |-
This text has
no trailing newline
# Keep trailing newline
text: |+
This text has
a trailing newline
# Strip leading whitespace
text: |2
This text has
indentation stripped
Numbers
YAML supports various number formats including integers, floating-point numbers, and scientific notation.
Integers
# Decimal integers
count: 42
negative: -10
zero: 0
# Octal (base 8)
octal: 0o755
# Hexadecimal (base 16)
hex: 0xFF
color: 0x1A2B3C
# Binary (base 2)
binary: 0b1010
Floating-Point Numbers
# Basic floats
price: 19.99
percentage: 85.5
negative_float: -3.14
# Scientific notation
large: 1.23e+10
small: 1.23e-10
scientific: 6.022e23
# Special float values
infinity: .inf
negative_infinity: -.inf
not_a_number: .nan
Number Examples in Context
server:
port: 8080
timeout: 30.5
max_connections: 1000
memory_limit: 1.5e9 # 1.5 GB
database:
version: 13.2
max_retries: 3
connection_pool: 0x20 # 32 in hex
Booleans
YAML supports multiple ways to represent boolean values, making it flexible for different use cases.
Standard Boolean Values
# Standard true/false
is_active: true
is_deleted: false
debug_mode: true
production: false
Alternative Boolean Values
# Yes/No
enabled: yes
disabled: no
feature_flag: yes
# On/Off
power: on
maintenance: off
service: on
# All of these are equivalent to true/false
boolean_examples:
true_values: [true, yes, on, True, Yes, On, TRUE, YES, ON]
false_values: [false, no, off, False, No, Off, FALSE, NO, OFF]
⚠️ Important Note:
YAML is case-sensitive for boolean values. True and true are different - only lowercase values are recognized as booleans.
Arrays (Lists)
Arrays in YAML can be written in block style (with dashes) or flow style (with brackets).
Block Style Arrays
# Simple list
fruits:
- apple
- banana
- orange
# List of objects
users:
- name: John
age: 30
- name: Jane
age: 25
# Nested lists
matrix:
- [1, 2, 3]
- [4, 5, 6]
- [7, 8, 9]
Flow Style Arrays
# Inline arrays
colors: [red, green, blue]
numbers: [1, 2, 3, 4, 5]
# Mixed types
mixed: [1, "hello", true, null]
# Nested flow arrays
coordinates: [[1, 2], [3, 4], [5, 6]]
Empty Arrays
# Empty block style
empty_list: []
# Empty flow style
empty_flow: []
# Both are equivalent
Objects (Maps)
Objects in YAML are collections of key-value pairs, similar to dictionaries or hash maps in programming languages.
Block Style Objects
# Simple object
person:
name: John Doe
age: 30
email: john@example.com
# Nested objects
company:
name: "Acme Corp"
address:
street: "123 Main St"
city: "New York"
country: "USA"
employees:
- name: John
position: "Developer"
- name: Jane
position: "Designer"
Flow Style Objects
# Inline objects
person: {name: John, age: 30, city: New York}
# Nested flow objects
config: {database: {host: localhost, port: 5432}, server: {port: 8080}}
# Mixed with arrays
users: [{name: John, active: true}, {name: Jane, active: false}]
Complex Nested Structure
application:
name: "My App"
version: "2.1.0"
settings:
database:
host: localhost
port: 5432
credentials:
username: admin
password: secret
features:
- authentication
- logging
- monitoring
limits:
max_users: 1000
timeout: 30
Null Values
YAML provides several ways to represent null or empty values.
Null Value Representations
# Explicit null
value: null
# Tilde notation
empty: ~
# Empty value (no value after colon)
missing:
# All three are equivalent and represent null/undefined
Null in Arrays and Objects
# Null in arrays
items: [apple, null, banana, null]
# Null in objects
user:
name: John
middle_name: null
last_name: Doe
nickname: # This is also null
# Mixed null values
data:
present: "some value"
missing: null
empty: ~
undefined:
Type Coercion and Quoting
YAML automatically detects data types, but sometimes you need to force a specific type using quotes.
Automatic Type Detection
# These are detected as numbers
count: 42
price: 19.99
scientific: 1.23e10
# These are detected as booleans
enabled: true
disabled: false
# These are detected as strings
name: John
version: 1.0.0
Forcing String Type with Quotes
# Force numbers to be strings
zip_code: "12345" # String, not number
phone: "555-1234" # String with dashes
version: "2.0" # String version
# Force booleans to be strings
flag: "true" # String "true", not boolean true
status: "yes" # String "yes", not boolean true
# Force null to be string
empty: "null" # String "null", not null value
Special Values That Need Quoting
# Values that look like other types
date_like: "2023-12-25" # Looks like date, quote to keep as string
number_like: "123" # Looks like number, quote to keep as string
boolean_like: "yes" # Looks like boolean, quote to keep as string
null_like: "null" # Looks like null, quote to keep as string
# Values with special characters
path: "C:\\Users\\John" # Backslashes
regex: "^[a-zA-Z]+$" # Special regex characters
url: "https://example.com" # Colons and slashes
Practice Exercises
Test your understanding with these practical exercises:
Exercise 1: Mixed Data Types
Create a YAML file representing a product catalog with different data types.
Show Solution
products:
- id: 1
name: "Laptop"
price: 999.99
in_stock: true
tags: [electronics, computer, portable]
specifications:
weight: 2.5
dimensions: [35.5, 24.0, 2.0]
warranty: "2 years"
reviews: null
Exercise 2: Configuration File
Create a server configuration with nested objects, arrays, and various data types.
Show Solution
server:
name: "Production Server"
environment: "prod"
debug: false
port: 8080
ssl:
enabled: true
certificate: "/etc/ssl/cert.pem"
key: "/etc/ssl/key.pem"
databases:
- name: "users"
host: "db1.example.com"
port: 5432
- name: "logs"
host: "db2.example.com"
port: 5432
features: [auth, logging, monitoring, caching]