velkaro.Runs in your browser

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.

By the Velkaro team · Published

The rules

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
The YAML to JSON converter with the comment test file on the left and the JSON on the right; the password value is "abc" and timeout is null.
The test file in the converter. In the JSON column, password is "abc" and timeout is null; retries does not appear at all.
LineJSON valueWhy
replicas: 2 # raise to 4…2A 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: #30nullThe value is empty and the rest is a comment. An empty value is null.
# retries: 5key not presentA 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:

The converter report reading "8 comments removed — JSON has no comments".
The report under the JSON: 8 comments removed. It counts the three header lines, the four end-of-line comments and the commented-out 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: 2

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.

Open the YAML to JSON converter

Sources

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.