YAML Data Types
Every value stored in a YAML file has a data type. A data type tells the computer what kind of value it is — a number, a word, a yes/no answer, or something empty. YAML automatically detects data types based on how the value is written. This automatic detection is called implicit typing.
Understanding YAML data types helps in writing correct configuration files and avoids unexpected behavior when tools read YAML values.
Overview of YAML Data Types
| Data Type | Category | Example Value |
|---|---|---|
| String | Scalar | Hello, "World" |
| Integer | Scalar | 42, -10 |
| Float | Scalar | 3.14, -0.5 |
| Boolean | Scalar | true, false |
| Null | Scalar | null, ~ |
| Sequence (List) | Collection | - apple, - mango |
| Mapping (Dictionary) | Collection | name: Alice |
| Date / Timestamp | Scalar | 2024-01-15 |
String
A string is a sequence of characters — letters, words, sentences, or symbols. Strings do not need quotes unless they contain special characters.
city: Mumbai greeting: Hello World message: "Error: File not found" note: 'It''s a good day'
Single quotes and double quotes both work for strings. Inside single-quoted strings, a single quote is written as two single quotes (''). Inside double-quoted strings, special characters use backslash escapes.
Integer
An integer is a whole number — no decimal point. YAML also supports integers in different number systems.
age: 25 temperature: -10 hex_value: 0xFF # Hexadecimal octal_value: 0o17 # Octal
YAML reads 25 as a number automatically. If the value must be treated as a string instead, wrap it in quotes: age: "25".
Float
A float is a number with a decimal point. YAML also supports scientific notation and special float values.
price: 9.99 pi: 3.14159 temperature: -0.5 large: 1.5e+10 # Scientific notation infinity: .inf # Positive infinity not_a_number: .nan # Not a Number
Boolean
A boolean represents a true or false value. YAML supports multiple ways to write booleans.
is_active: true is_deleted: false
In YAML 1.1 (older version), values like yes, no, on, and off were also treated as booleans. In YAML 1.2 (current version), only true and false are official boolean values. Always use true and false for reliability.
Boolean Trap Example
# In YAML 1.1 (older parsers) flag: yes # Parsed as true flag: no # Parsed as false # Safe approach for all parsers flag: true flag: false
Null
A null value means the key exists but has no value. YAML supports two ways to write null.
middle_name: null nickname: ~ empty_field: # An empty value is also treated as null
All three lines above store a null value. The tilde ~ is a shorthand for null in YAML.
Date and Timestamp
YAML supports ISO 8601 date and timestamp formats natively.
birthday: 1995-06-15 created_at: 2024-01-15T09:30:00Z updated_at: 2024-01-15 09:30:00 +05:30
Dates follow the YYYY-MM-DD format. Timestamps include time and timezone information. YAML parsers automatically convert these strings into date objects in most programming languages.
Data Type Detection Diagram
+-------------------------------+------------------+ | YAML Value Written | Type Detected | +-------------------------------+------------------+ | name: Alice | String | | count: 10 | Integer | | ratio: 0.75 | Float | | active: true | Boolean | | nickname: null | Null | | dob: 2000-05-20 | Date | | count: "10" | String (forced) | +-------------------------------+------------------+
Forcing a Data Type with Tags
YAML allows explicit data type declaration using tags. Tags start with !! followed by the type name.
zip_code: !!str 400001 # Force integer-looking value to be a string count: !!int "42" # Force string to be an integer flag: !!bool "true" # Explicit boolean
Tags are useful when a value looks like one type but must be treated as another. For example, PIN codes and ZIP codes look like integers but should be stored as strings to preserve leading zeros.
Real-World Example
pin_code: !!str 007654 # Stored as "007654", not 7654
Without the !!str tag, YAML would parse 007654 as the integer 7654, dropping the leading zero.
Collections Are Also Data Types
Lists (sequences) and dictionaries (mappings) are also data types in YAML. They are called collection types. These are covered in detail in the upcoming topics on Lists and Mappings.
# Sequence (List) fruits: - apple - mango - banana # Mapping (Dictionary) person: name: Alice age: 25
Summary
- YAML automatically detects data types based on the written value
- Main scalar types are: String, Integer, Float, Boolean, Null, and Date
- Use
!!str,!!int,!!booltags to force a specific type - Use
trueandfalsefor booleans to ensure compatibility across parsers - Null is written as
null,~, or left empty - Date values follow the ISO 8601 format:
YYYY-MM-DD
