YAML vs. JSON: What’s the difference between YAML and JSON?
YAML and JSON are both popular data serialization languages. Both have similar functions and features that allow us to present data objects and structures using a simple syntax.
Does YAML have re-usable variables?
Yes, we can reuse scalar nodes that we have written in YAML. This is done the same way we added those scalar nodes to the end of the path. However, we cannot change what’s already inside the scalar nodes.
What’s the difference between .yaml vs .yml file extensions?
Nothing! Both are valid according to the YAML spec, however .yaml is the recommended pattern. The shorter .yml was born out of the fact that older versions of Windows restricted file extensions to three characters. You may encounter situations where code generation or other tooling favors one over the other though so be sure to check!
I have a very long string, how can I break it over several lines?
There are a few ways of accomplishing this. First, you can use the “literal” operator | to preserve text formatting. The other main style is by using the “folding” operator > that will replace line breaks with spaces in consecutive lines. We cover this in more detail with examples here.
Do I need quotes around strings?
The short answer is no, you do not always need quotes around strings in YAML. There are some exceptions to this however:
number: 15will treat 15 as an integer while
number: “15”will explicitly treat it as a string.
- You must use quotes if your string contains special characters like !, *, ?, etc.
- Double quotes allow you to properly insert escape characters like
\ninto your string.
- Single quotes will allow you to insert escape characters into a string literally.
Can I write block comments in YAML?
YAML does not support block comments, only single-line comments using #. To simulate a block comment you’ll have to start each line with the # character.
Can I convert YAML to JSON and vice-versa?
Yes, you can convert between the two. There are several online resources for doing this manually and several libraries for doing it programmatically.
Can YAML contain tabs?
No, tab characters are forbidden by YAML. Indentation can be achieved by using either 2 or 4 spaces. Most editors can be configured to use spaces in place of tab characters when you press your keyboard’s tab key. The reason for this is that tabs are treated differently by different editors and parsers.