What Is YAML? Syntax, Data Types, Uses, and Pitfalls
Scrapeless Scraping API returns structured web data in JSON or CSV, which a controlled pipeline can transform into YAML when a human-edited configuration boundary needs it.
TL;DR
- YAML is a human-oriented data serialization language. It represents mappings, sequences, and scalar values with indentation and limited punctuation.
- YAML is common in configuration files. Comments, readable keys, block strings, and compact lists make it convenient for settings reviewed in source control.
- Whitespace carries structure. Spaces establish nesting, while tab handling and inconsistent indentation can produce errors or surprising data.
- Parser behavior must be controlled. YAML versions, schemas, tags, duplicate keys, and implicit typing can change how text becomes application values.
- YAML is not ideal for every data path. JSON is often clearer for APIs, and typed binary or columnar formats are better for large analytical datasets.
What Does YAML Mean?
YAML means “YAML Ain’t Markup Language.” It is a Unicode-based data serialization language designed to map cleanly to common programming structures. A YAML document is built from three main node kinds: mappings for key-value associations, sequences for ordered collections, and scalars for individual values.
The YAML 1.2.2 specification describes the representation model, serialization tree, presentation stream, syntax, tags, and schemas. YAML’s presentation layer offers several ways to write equivalent data, which helps human authors but gives implementers more choices than JSON’s smaller grammar.
YAML is data, not an instruction language by definition. Applications frequently use it to configure deployments, build systems, automation jobs, static-site metadata, and local developer tools. The application decides which keys are valid and what those keys do.
Basic YAML Syntax
A YAML configuration can combine mappings, sequences, numbers, booleans, and nested values:
service:
name: catalog-worker
enabled: true
workers: 4
regions:
- us-east
- eu-west
output:
format: json
include_metadata: true
The colon separates a mapping key from its value. A dash introduces a sequence item in block style. Indentation places name, enabled, and the remaining settings under service. The document does not need braces or commas in this style.
Mappings
A mapping associates keys with values. Keys are commonly plain strings, although the language model permits more complex keys. Configuration authors should prefer simple, unique string keys because application libraries and validation tools handle them predictably.
Duplicate mapping keys are a portability problem. Libraries may reject them, keep one value, or expose behavior controlled by an option. A production configuration loader should reject duplicates so a reviewer and the running application see the same effective setting.
Sequences
A sequence is ordered. Block style uses one dash per item, while flow style uses brackets:
regions: [us-east, eu-west]
checks:
- name: schema
required: true
- name: links
required: true
Each item can be a scalar, mapping, or another sequence. Order should be used only when the application contract assigns meaning to it.
Scalars and Quoting
Scalars include strings, numbers, booleans, nulls, timestamps under some schemas, and values with explicit tags. Plain scalars omit quotes, but punctuation and certain words can be interpreted in unexpected ways across YAML versions or parser schemas. Quote values when their textual form must remain exact, including version numbers, identifiers with leading zeros, and strings that resemble booleans or null.
Single-quoted strings treat most characters literally. Double-quoted strings support escape sequences. Block scalars use | to preserve line breaks or > to fold lines into spaces, with additional indicators controlling indentation and trailing line breaks.
Comments, Anchors, Aliases, and Tags
A hash character begins a comment outside a quoted scalar. Comments make YAML attractive for configuration because maintainers can explain why a setting exists. Comments are part of presentation rather than the core data model, so many parsers discard them when loading and writing a document.
An anchor labels a node, and an alias refers to that node elsewhere. This can reduce repeated configuration:
defaults: &defaults
timeout_seconds: 30
output: json
jobs:
catalog:
<<: *defaults
region: us-east
The merge key shown here is widely used, but merge behavior is not a simple universal feature of every YAML processing path. Confirm what the chosen library and application support. Excessive anchor graphs also make configuration review harder because the effective value is no longer visible in one place.
Tags identify the type or interpretation of a node. Standard tags cover strings, integers, mappings, sequences, and other core values. Some libraries support application-specific tags that construct language objects. Loading untrusted tags into object constructors can be dangerous; use a safe loader that limits construction to expected data types.
YAML Schemas and Version Differences
A YAML schema determines how scalar text resolves to tags. Version and schema differences explain many surprising examples found in configuration repositories. A word interpreted as a boolean by one processing mode may remain a string under another. Numeric syntax and timestamp handling can vary as well.
YAML 1.2 aligned its JSON schema so JSON documents are valid YAML in the intended compatibility model. That does not mean every YAML file is valid JSON: comments, unquoted keys, block collections, anchors, aliases, and tags are YAML features outside JSON syntax.
The YAML media type specification registers application/yaml and the +yaml structured syntax suffix. A file extension alone does not state the parser schema or application contract. Projects should pin the parser library, document the supported YAML version or subset, and validate loaded data against an application schema.
YAML vs JSON
| Dimension | YAML | JSON |
|---|---|---|
| Primary strength | Human-authored configuration and readable structured documents | Predictable machine exchange and web APIs |
| Structure | Indentation, block style, or flow style | Braces, brackets, commas, and quoted property names |
| Comments | Supported | Not part of standard JSON |
| References | Anchors and aliases | No native reference syntax |
| Parsing surface | Broad grammar with schemas, tags, and multiple presentation styles | Smaller grammar and fewer representation choices |
| Common file use | Settings, manifests, build and deployment configuration | API payloads, events, application state, configuration |
| Streaming records | Supports multi-document streams, but application conventions vary | Needs framing such as arrays or NDJSON |
Common YAML Use Cases
Application Configuration
Readable nested settings, comments, and lists work well when developers review changes in version control.
Infrastructure Manifests
Declarative systems use YAML to describe desired resources, policies, relationships, and deployment parameters.
Automation Pipelines
Build and delivery tools often use YAML to list stages, jobs, dependencies, environments, and conditions.
Document Front Matter
Static-site and publishing tools place a small YAML mapping beside human-written content to declare titles, tags, and layout options.
YAML Security and Reliability
Untrusted YAML should be treated as untrusted structured input. Use a safe-loading mode that constructs only ordinary data values, not language-specific objects or application classes. The general risk belongs to the broader category described by CWE-502: Deserialization of Untrusted Data.
Set limits on input size, nesting depth, aliases, and aggregate expansion. A small document can refer to anchored values many times, causing a loader to create a much larger in-memory structure. Libraries expose different controls, so tests should exercise the deployed parser with representative limits.
Validate the loaded data after parsing. Reject unknown top-level keys when configuration mistakes would be dangerous. Check required fields, allowed enum values, numeric ranges, path restrictions, and relationships between settings. Do not log secrets or the full configuration on a validation error.
Common YAML Mistakes
- Using tabs for indentation. Prefer spaces and enforce one indentation width through editor settings and a formatter.
- Leaving ambiguous scalars unquoted. Quote identifiers and text that resembles a boolean, null, number, or timestamp.
- Allowing duplicate keys. Configure the loader or linter to reject them.
- Assuming comments survive round trips. Many object-based loaders discard comments and original formatting.
- Overusing anchors and merges. Reuse can reduce repetition, but hidden effective values make reviews and overrides difficult.
- Skipping schema validation. A well-formed document can still contain misspelled keys or unsafe values.
How to Use YAML Well
- Define a small supported subset. Decide whether the project permits anchors, merge keys, custom tags, multi-document streams, and flow style.
- Pin the parser and behavior. Record the library, supported YAML version, duplicate-key policy, and safe-loading mode.
- Add linting and schema checks. Run them before deployment so indentation errors and unknown keys fail early.
- Keep secrets outside committed YAML. Reference environment-provided or secret-manager values according to the application’s documented mechanism.
- Review effective configuration. When inheritance or merging exists, provide a command that renders the final settings without exposing secret values.
- Use another format when the boundary changes. Prefer JSON for public APIs and typed analytical formats for large datasets.
Conclusion
YAML is a flexible serialization language whose readable block style makes it especially useful for configuration. Its convenience comes with a larger interpretation surface: indentation, schemas, tags, anchors, aliases, duplicate keys, and parser options can all affect the loaded values. A dependable YAML workflow narrows the supported feature set, uses safe loading, validates the resulting data, and keeps human-friendly configuration separate from high-volume machine exchange.
Ready to Build a Structured Configuration Workflow?
Collect structured web data with Scrapeless Scraping API, then transform only the validated values your configuration contract permits.
Sign up today and get $5 in free credit — no credit card required.
Claim Your $5 Credit →FAQ
Is YAML a programming language?
No, YAML is a data serialization language. An application may interpret YAML keys as instructions, but that behavior belongs to the application rather than YAML itself.
Is YAML a superset of JSON?
YAML 1.2 was designed so JSON syntax fits within its compatibility model, but real parser support and edge cases depend on versions and implementations. YAML-specific syntax is not valid JSON.
Why does indentation matter in YAML?
Indentation defines parent-child structure in block-style YAML. Changing spaces can move a value into another mapping or sequence, or make the document invalid.
Can YAML contain comments?
Yes, YAML supports comments beginning with a hash outside quoted scalars. Many parsers discard comments during a load-and-write round trip.
Is it safe to parse YAML from an untrusted source?
Untrusted YAML requires a safe loader, resource limits, disabled application-specific object construction, and schema validation. Never use a general object-deserialization mode on untrusted input.