Strings in YAML - To Quote or not to Quote | tinita [blogs.perl.org]

submited by
Style Pass
2023-01-24 20:30:05

YAML is a data serialization language, and one design goal was that it's human friendly. It should be easy to read and edit, even if that makes parsing it harder.

If you look at JSON, you have only one style to encode strings, and that's the double quoted style which doesn't allow literal linebreaks.

YAML files are used for many different purposes, and there are many types of strings, especially multiline strings. For each use case, you can choose the type of quoting (or no quoting) that makes the string readable and easy to edit.

The good news is, the YAML double quoted string works the same as in JSON, so if you know that already, you will be able to write correct YAML. (I should note, though, that it also depends on the processor you use, since not all are fully JSON compatible. The incompatible cases should be rare, though.)

In YAML, you can write a string without quotes, if it doesn't have a special meaning. See the next section for cases where you have to quote a string.

Leave a Comment