YAML: YAML Ain't Markup Language

YAML is a superset of JSON, so cover JSON first!

Main design goals:

  1. Easy for humans to read
  2. Portable between programming languages

Uses indented blocks similar to Python source code.

Basic notation

Key-value pairs:

alpha: beta
gamma: delta

or (compact style)

{alpha: beta, gamma: delta}

or (for complex substructure)

alpha:
  beta
gamma:
  delta

Sequences (lists):

- alpha
- beta

or (compact style)

[alpha, beta]

or (for complex substructure)

-
  alpha
-
  beta

Multiple "documents" can be included in a single YAML file or stream: --- marks the beginning of a document; ... marks the end of stream (i.e., end of the last document).

Comments begin with # and extend to the end of a line.

Text with multiple lines can be marked | (preserve newlines) or > (wrap newlines).

Example

Cross-references

&NAME makes NAME a label (anchor, identifier).

*NAME is a reference to NAME

Example

- pet: &pet001
    breed: mouse
    name: Micky
    weight: 0.2
    sex: male
    price: 1025.00
    spouse: *pet002
    description: Famous, talking mouse, married to Minnie.
- pet: &pet002
    breed: mouse
    name: Minnie
    weight: 0.3
    sex: female
    price: 1050.00
    spouse: *pet001
    description: Micky's adorable companion.

Comparisons

Compared to JSON, YAML is easier for humans to read and understand, harder for programs to parse.

YAML is a superset of JSON. What does this mean? What does YAML have that JSON doesn't?

Cross-references allow arbitrarily linked structures (graphs). Is this possible in JSON? In XML?

Miscellaneous

Python usage: install python-yaml, then import yaml.

No idea here of validation?

References