Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spaces in YAML keys

Tags:

yaml

Is there a correct way to use spaces in YAML keys? Like

a test: "hello world!" 

or

"a test": "hello world!"

or is it just a bad idea and one should use

a_test: "hello world!"

All of them seem valid in a yaml-Linter, but I didn't find any examples online using spaces in a key.

like image 746
Paul Würtz Avatar asked Dec 05 '16 15:12

Paul Würtz


People also ask

Can YAML keys have spaces?

Spaces are allowed in keys according to the specification and for those you don't need quotes (double or single, each with their own use). It is just a scalar string containing a space.

How do you escape spaces in YAML?

In YAML, \n is used to represent a new line, \t is used to represent a tab, and \\ is used to represent the slash. In addition, since whitespace is folded, YAML uses bash style "\ " to escape additional spaces that are part of the content and should not be folded.

How do I fix indentation error in YAML?

A YAML file use spaces as indentation, you can use 2 or 4 spaces for indentation, but no tab. In other words, tab indentation is forbidden: Why does YAML forbid tabs? Tabs have been outlawed since they are treated differently by different editors and tools.

How do I use special characters in YAML?

Use quotes in YAML if your value includes special characters. For example, these special characters may require quotes: {, }, [, ], ,, &, :, *, #, ?, |. -, <. >, =, !, %, @, \. It is not necessary to use quotes when a single special character is surrounded by spaces, for example, * with spaces on both sides.


1 Answers

One thing is what is allowed, another is what is human readable.

Spaces are allowed in keys according to the specification and for those you don't need quotes (double or single, each with their own use). It is just a scalar string containing a space.

As for human readability, I tend to think of a and test in a test as not belonging together. That is of course caused by what I am used to, and that e.g. variables in most programming languages cannot have spaces (although my one of my first languages, Algol 68, did allow that). For human readability I would suggest using a_test over "a test" (or 'a test'), but others might have different preferences.

like image 70
Anthon Avatar answered Oct 22 '22 06:10

Anthon