Skip to main content
riqo.ioTools for big ideas

YAML: syntax, types and structures

A guide to YAML as a human-readable serialization format: scalars, collections, indentation, anchors and parsing pitfalls.

Concept and context

YAML is a serialization format designed for human readability and is widely used for configuration and declarative documents.

Its compact notation removes much of JSON's punctuation, but indentation and context become semantically important.

A sound mental model separates the abstract concept from its concrete representation and from the environment in which it is used. That separation prevents assumptions that are valid for one protocol, library or format from being carried into systems whose rules or guarantees are different.

Fundamentals and terminology

YAML nodes can be scalars, sequences or mappings, and scalars may use plain, quoted, literal or folded styles.

Parser version and resolution schema influence whether apparently simple text is interpreted as a boolean, number, date or string.

Terminology should be read together with the standard, version or contract that defines it, because similar words can describe different properties at different layers. Making those definitions explicit improves interoperability, documentation and the ability to diagnose unexpected behavior.

How it works

Indentation establishes hierarchy while dashes and colons introduce sequences and mappings.

Anchors and aliases can reuse nodes, but they add complexity and parsers often enforce limits to prevent excessive alias expansion.

In real systems it helps to follow data across layers and identify which transformations are reversible, which introduce constraints and where information can be lost. This makes responsibilities among producers, consumers, storage and transport easier to reason about and test.

Worked example

A configuration containing services, ports and environment variables demonstrates nested structures with little punctuation.

Values such as 00123, yes or date-like text may nevertheless be resolved differently than intended unless quoting and parser behavior are understood.

A worked example becomes reusable when it exposes its preconditions and invariants rather than showing only an end result. Changing one assumption at a time helps distinguish behavior guaranteed by a standard from choices made by a particular application or implementation.

Errors and misconceptions

YAML is not merely JSON without braces, and implementations differ in support for tags, anchors and specification versions.

Indentation errors, tabs, duplicate keys and implicit type coercion are frequent sources of subtle configuration defects.

Many failures come from implicit assumptions between systems that look compatible while using different versions, canonicalization rules or type models. For interoperability and security, unusual inputs should therefore be specified and tested deliberately instead of being treated as irrelevant edge cases.

Best practices and selection criteria

Pin a known YAML version and library, avoid advanced constructs when interoperability matters and quote ambiguous values.

JSON is often more predictable for strict machine exchange, while YAML is useful when humans routinely author and review configuration.

Robust practice combines documented standards, mature libraries, explicit contracts and tests that include representative boundary cases. The best choice is not automatically the shortest or most popular one; portability, readability, performance, security, evolution and operating cost all matter.

Related guides

JSON vs YAML vs XML: choosing the right format

A reasoned comparison of JSON, YAML and XML for APIs, configuration, documents, schemas and interoperability.

XML: elements, attributes, namespaces and documents

XML fundamentals: tree structure, attributes, namespaces, encoding, validation and interoperability.

Data serialization and deserialization

How in-memory structures become transferable representations and what can break across types, versions and systems.

JSON Schema: describing and validating data structures

How JSON Schema expresses types, properties, constraints, composition and compatibility for JSON documents.