Syntax A cheat sheet and full specification are available at the official site. The following is a synopsis of the basic elements. YAML accepts the entire Unicode character set, except for some
control characters, and may be encoded in any one of
UTF-8,
UTF-16 or
UTF-32. (Though UTF-32 is not mandatory, it is required for a parser to have
JSON compatibility.) •
Whitespace indentation is used for denoting structure; however,
tab characters are not allowed as part of that indentation. •
Comments begin with the
number sign (), can start anywhere on a line and continue until the end of the line. Comments must be separated from other tokens by whitespace characters. If characters appear inside of a string, then they are number sign () literals. • List members are denoted by a leading
hyphen () with one member per line. • A list can also be specified by enclosing text in
square brackets () with each entry separated by a
comma. • An
associative array entry is represented using
colon space in the form
key: value with one entry per line. YAML requires the colon be followed by a space so that url-style strings like can be represented without needing to be enclosed in quotes. • A
question mark can be used in front of a key, in the form "?key: value" to allow the key to contain leading dashes, square brackets, etc., without quotes. • An associative array can also be specified by text enclosed in
curly braces ({{code|{...} }}), with keys separated from values by colon and the entries separated by commas (spaces are not required to retain compatibility with JSON). •
Strings (one type of scalar in YAML) are ordinarily unquoted, but may be enclosed in
double-quotes (), or
single-quotes (). • Within double-quotes, special characters may be represented with
C-style escape sequences starting with a
backslash (). According to the documentation the only octal escape supported is . • Within single quotes the only supported escape sequence is a doubled single quote () denoting the single quote itself as in . • Block scalars are delimited with
indentation with optional modifiers to preserve (|) or fold () newlines. • Multiple documents within a single stream are separated by three
hyphens (). • Three
periods () optionally end a document within a stream. • Repeated nodes are initially denoted by an
ampersand () and thereafter referenced with an
asterisk (). • Nodes may be labeled with a type or tag using a double
exclamation mark () followed by a string, which can be expanded into a URI. • YAML documents in a stream may be preceded by "directives" composed of a
percent sign () followed by a name and space-delimited parameters. Two directives are defined in YAML 1.1: • The %YAML directive is used for identifying the version of YAML in a given document. • The %TAG directive is used as a shortcut for URI prefixes. These shortcuts may then be used in node type tags.
Basic components Conventional block format uses a hyphen+space to begin a new item in list. --- # Favorite movies - Casablanca - North by Northwest - The Man Who Wasn't There Optional inline format is delimited by comma+space and enclosed in brackets (similar to
JSON). --- # Shopping list [milk, pumpkin pie, eggs, juice] Keys are separated from values by a colon+space. Indented blocks, common in YAML data files, use indentation and new lines to separate the key/value pairs. Inline blocks, common in YAML data streams, use comma+space to separate the key/value pairs between braces. --- # Indented Block name: John Smith age: 33 --- # Inline Block {name: John Smith, age: 33} Strings do not require quotation marks. There are two ways to write multi-line strings, one preserving newlines (using the | character) and one that folds the newlines (using the character), both followed by a newline character. data: | There once was a tall man from Ealing Who got on a bus to Darjeeling It said on the door "Please don't sit on the floor" So he carefully sat on the ceiling By default, the leading indentation (of the first line) and trailing whitespace are stripped, though other behavior can be explicitly specified. data: > Wrapped text will be folded into a single paragraph Blank lines denote paragraph breaks Folded text converts newlines to spaces and removes leading whitespace. --- # The Smiths - {name: John Smith, age: 33} - name: Mary Smith age: 27 - [name, age]: [Rae Smith, 4] # sequences as keys are supported --- # People, by gender men: [John Smith, Bill Jones] women: - Mary Smith - Susan Williams Objects and lists are important components in yaml and can be mixed. The first example is a list of key-value objects, all people from the Smith family. The second lists them by gender; it is a key-value object containing two lists.
Advanced components Features that distinguish YAML from the capabilities of other data-serialization languages are structures, and data and composite keys. YAML structures enable storage of multiple documents within a single file, usage of references for repeated nodes, and usage of arbitrary nodes as keys. --- # Transform between two systems of coordinates transform: {x: 1, y: 2}: {x: 3, y: 4} {x: 5, y: 6}: {x: 7, y: 8}
Example Data-structure hierarchy is maintained by outline indentation. --- receipt: Oz-Ware Purchase Invoice date: 2012-08-06 customer: first_name: Dorothy family_name: Gale items: - part_no: A4786 descrip: Water Bucket (Filled) price: 1.47 quantity: 4 - part_no: E1628 descrip: High Heeled "Ruby" Slippers size: 8 price: 133.7 quantity: 1 bill-to: &id001 street: | 123 Tornado Alley Suite 16 city: East Centerville state: KS ship-to: *id001 specialDelivery: > Follow the Yellow Brick Road to the Emerald City. Pay no attention to the man behind the curtain. ... Notice that strings do not require enclosure in quotation marks. The specific number of spaces in the indentation is unimportant as long as parallel elements have the same left justification and the hierarchically nested elements are indented further. This sample document defines an associative array with 7 top level keys: one of the keys, "items", contains a 2-element list, each element of which is itself an associative array with differing keys. Relational data and redundancy removal are displayed: the "ship-to" associative array content is copied from the "bill-to" associative array's content as indicated by the anchor () and reference () labels. Optional blank lines can be added for readability. Multiple documents can exist in a single file/stream and are separated by . An optional can be used at the end of a file (useful for signaling an end in streamed communications without closing the pipe). ==Features==