Basic YAML
Basic YAML writes data like JSON, with less punctuation: indentation instead of braces, line breaks instead of commas, and quotes only where a string needs them.
Mappings
name: go-yaml
license: Apache-2.0
stars: 42
{
"name": "go-yaml",
"license": "Apache-2.0",
"stars": 42
}A mapping holds key: value pairs, one per line, and reads as a JSON object. The space after the colon is part of the
syntax: key:value with no space is one string.
Sequences
- one
- two
- three
[
"one",
"two",
"three"
]A sequence holds one entry per - line, and reads as a JSON array.
Nesting
service:
name: api
ports:
- 80
- 443
owner:
team: platform
{
"service": {
"name": "api",
"ports": [
80,
443
],
"owner": {
"team": "platform"
}
}
}Indentation nests one collection inside another. Use spaces: a tab is never indentation in YAML, and go-yaml rejects a document indented with one. Any number of spaces works, as long as the entries of one collection line up.
A sequence under a key may also start at the key’s own column, ports: over - 80. Both layouts read the same.
Scalars
plain: hello world
quoted: "hello world"
single: 'it''s'
integer: 42
float: 3.14
boolean: true
nothing: null
tilde: ~
empty:
{
"plain": "hello world",
"quoted": "hello world",
"single": "it's",
"integer": 42,
"float": 3.14,
"boolean": true,
"nothing": null,
"tilde": null,
"empty": null
}A value written without quotes is a plain scalar, and YAML reads its type from its text:
42is an integer and3.14a float.trueandfalseare booleans.null,~and an empty value are null.- Anything else is a string.
You only need to quote a string when its text would read as something else: "42", "true", "null". A double-quoted string takes
escapes such as \n. A single-quoted string takes none, and writes a quote as two: 'it''s'.
These rules are YAML 1.2’s. YAML 1.1 reads more words as booleans, among them yes, no, on and off; see
YAML versions.
Comments
# The whole line is a comment.
port: 8080 # So is the rest of this one.
url: http://example.com/#anchor
{
"port": 8080,
"url": "http://example.com/#anchor"
}A comment starts at a # that opens the line or follows a space, and runs to the end of the line. JSON has no comments,
so the JSON drops them. The # in the URL follows no space, so it belongs to the string.
JSON is YAML
{"name": "go-yaml", "tags": ["yaml", "go"], "stable": false}
{
"name": "go-yaml",
"tags": [
"yaml",
"go"
],
"stable": false
}Since YAML 1.2, a JSON document is a YAML document: braces and brackets are YAML’s flow style, the one-line form of a mapping and a sequence.
tags: [yaml, go]
owner: {team: platform, size: 4}
{
"tags": [
"yaml",
"go"
],
"owner": {
"team": "platform",
"size": 4
}
}The two styles mix. A flow collection inside a block mapping keeps short lists on one line, and its strings need no quotes either.