Comments are used to provide additional information or notes about the data in a YAML document. They are ignored by the parser and do not affect the data in any way. Comments can be used to provide context, explain the purpose of certain values, or simply to add personal notes for yourself or other developers.
To add a comment in YAML, simply begin the line with the “#” symbol. The parser will ignore anything that follows the “#” symbol on that line. Here is an example:
# This is a comment person: name: John # This is another comment age: 30
In this example, the first line is a comment that is ignored by the parser. The third line contains a comment that follows a key-value pair.
It’s important to note that comments can only appear on lines by themselves or after a key-value pair. Comments cannot be added after a value on the same line. Here is an example of an invalid comment:
person: name: John # This is a comment on the same line as the value - this is invalid age: 30
In this example, the comment after “John” is invalid because it is on the same line as the value. The parser will not recognize it as a comment and will throw an error.
Follow us on social media