YAML field notes · 01
YAML comments: syntax, multi-line comments, and what happens in JSON
A YAML comment starts with # and runs to the end of the line. The # has to be at the start of a line or after a space or tab, there is no block-comment syntax (a multi-line comment is several # lines), and comments are not data: convert the file to JSON and all 8 comments in our test file are gone.
The rules
- Whole-line comment:
# text, at any indentation. - End-of-line comment:
replicas: 2 # text. The YAML 1.2.2 specification says comments “must be separated from other tokens by white space characters” (section 6.6). Without the space,#is just a character in the value. - Multi-line comment: start every line with
#. There is no/* … */form; the specification’s own example of multi-line comments is consecutive#lines. - Not a comment:
#inside single or double quotes, or anywhere inside a|or>block, is part of the text.
Seven lines to check in your own files
We ran this file through the YAML to JSON converter. The table says how each line was read; the last five are the ones people misread.
# Billing API settings
# Comments can start the file, sit on their own line,
# or follow a value after at least one space.
name: billing-api
replicas: 2 # raise to 4 before the sale
docs: "https://example.com/retries#backoff"
color: blue#2 # no space before #, so blue#2 is the value
password: abc #123 # space before #: the value is only abc
timeout: #30 # the whole value is a comment, so timeout is null
# retries: 5 # a commented-out key does not exist in the data
script: |
echo "deploy" # inside a block scalar, this is text
# and so is this line

password is "abc" and timeout is null; retries does not appear at all.Open this example in the converter →
| Line | JSON value | Why |
|---|---|---|
replicas: 2 # raise to 4… | 2 | A space comes before the #, so the rest of the line is a comment. |
docs: "https://…/retries#backoff" | "https://example.com/retries#backoff" | Inside quotes, # is text. Quote any URL that has a fragment. |
color: blue#2 | "blue#2" | No space before the #, so it is part of the value. |
password: abc #123 | "abc" | The space turns #123 into a comment and the value silently loses it. Nothing warns you; quote any value that can contain a space followed by # (passwords, tokens, free text). |
timeout: #30 | null | The value is empty and the rest is a comment. An empty value is null. |
# retries: 5 | key not present | A commented-out key is not data. The program reading the file uses its own default. |
script: | … # … | "echo \"deploy\" # inside a block scalar, this is text\n# and so is this line\n" | Lines inside a block scalar are text, including lines that start with #. |
The two lines worth checking in your own files are the password and timeout ones: both load without an error, and both give a value that is not what the author typed.
Converting to JSON: comments are dropped, not moved
JSON has no comment syntax at all. RFC 8259 builds a JSON text from six structural characters, strings, numbers and three literal names, and nothing else, so a converter has nowhere to put comments (the converter’s feature table quotes the YAML specification on this). Ours removes them and says so in the report under the output:

retries line.The loss is permanent. Send that JSON back through JSON to YAML and you get the data, with no comments and with the truncated password:
name: billing-api
replicas: 2
docs: https://example.com/retries#backoff
color: blue#2
password: abc
timeout: null
script: |
echo "deploy" # inside a block scalar, this is text
# and so is this line
This is not specific to converters. Loading the same file with PyYAML’s safe_load and writing it with safe_dump also returns it without a single comment, because both work on plain data. Tools that must keep comments while editing a file work on a document model instead; the yaml library for JavaScript, for example, attaches comments to nodes in its Document API.
If a note has to survive the conversion
Make it data. A key such as _comment goes through both converters unchanged:
{
"_comment": "replicas: raise to 4 before the sale",
"name": "billing-api",
"replicas": 2
}_comment: "replicas: raise to 4 before the sale"
name: billing-api
replicas: 2Open this JSON in the JSON to YAML converter →
This only works if the program that reads the file ignores keys it does not know. Check its schema first. Docker Compose, for instance, ignores fields that start with x-; its reference calls this “the sole exception” to rejecting unknown fields, so in a Compose file a note belongs under an x- key, not under _comment.
If the JSON is only an output (an API payload, a build artifact), keep the commented YAML as the file you edit and generate the JSON from it each time, rather than editing the JSON and losing the notes.
Sources
- YAML 1.2.2 specification, 6.6 Comments: white-space rule, comments as a presentation detail, multi-line comment example
- RFC 8259, The JSON Data Interchange Format: JSON tokens (section 2)
- Docker Docs, Compose file extensions: x- fields are ignored by Compose
- yaml library documentation: comments attached to nodes in the Document API
All pages were read on 10 November 2026. Converter outputs on this page are generated by the same code that runs in the converter; PyYAML results come from PyYAML 6.0.3 on Python 3.