Scalars

Scalars or variables are basic values that include strings, numbers, or booleans paired with an associated key name. Suppose we define scalars in YAML. In that case, we will need to create some key-value pairs and pay attention to the line separations, space, and indentation in our data structure writing.

Defining value example:

host: localhost-01  
contactcenter:  
  location: Argentina 
  cab: 11  
  cab_unit: 1

In the example above, we understand that the key host has the string value localhost-01, therefore we can recognize host: localhost-01 as a key-value pair. Essentially, a scalar is a single value number or simply just a value, and that makes the key  host and the string localhost-01, in each of their own, a scalar.

Output:

▾ object {2}     host: localhost-01
  ▾ contactcenter {3}
      location: Argentina
      cab: 11
      cab_unit: 1

Let’s take a look at the defining value example once again. We can see that both values for host and location are immediately recognized as strings in the output. In some cases, however, we can use single quotes ‘ or double quotes “ to turn any value into a string value. Take a look at the example below.

Turning numbers value into string example :

host: localhost-01  
contactcenter: 
  location: Brazil
  cab: “12” 
  cab_unit: ‘2’ 

In the example written above, we have the intention to change the number values into string values. In that case, we have put double quotes “ on the value 12 and single quotes ‘on the value 2. 

Output:

▾ object {2}     host: localhost-01
  ▾ contactcenter {3}
      location: Brazil
      cab: 12
      cab_unit: 2

In this output, we can see that the numbers value 12 and 2 have changed to strings value 12 and 2.

The Significance of Single Quotes and Double Quotes

If the previous chapter makes you wonder about the differences between the use of single quotes ‘ and double quotes “, please take a look at another example below.

Single quotes and double quotes example:

host: localhost-01 
contactcenter: 
  location: Brazil
  cab: “12\n” 
  cab_unit: ‘2\n’  

In the line of codes above, we have added the special character \n on the number values. Therefore, those number values are written as 12\n and 2\n. As you see, we have also added the double quotes ” and single quotes ‘ respectively.

Output:

▾ object {2}     host: localhost-01
  ▾ contactcenter {3}
      location: Brazil
      cab: 12
      cab_unit: 2\n

Here, we see the output for ‘2\n’ is 2\n, and it is not the output that we want, we want a clean output. As we can see from the example, we can get 12 without following the special symbol \n in the output. The output above is because we use single quotes for ‘2\n’ and single quotes ‘ only provide one escape pattern. Therefore, the special symbol \n is read as a string. Special symbols like \”, \0, \t, \u263A, \x0d\x0a == \r\n, and more needed double quotes ” to allow escape sequences. In this case, instead of writing ‘2\n’ we need to write “2\n” so that the special symbol will not be read as a string.

Multiline Strings

There are two ways of writing in multiline strings depending on how we intend to write the codes. If we want to write multiline strings with breaks for the output, we can use the pipe  |  symbol. On the other hand, if we want to write multiline strings in a single line for the output, we ought to use the closing angle bracket > symbol. You may refer to the example below.

Multiline strings with breaks example:

maintenance_sch: |
  2019-07-29 – software update patch 1.0.1 
  2020-05-12 – software update patch 1.0.2   2020-09-20 – software update patch 1.0.3   

In the example above, we have maintenance_sch key that is followed by we the pipe  |  symbol. In the following line, we have string values expected to have numerous lines depending on the line break.

Output:

▾ object {1}     maintenance_sch:
      2019-07-29 – software update patch 1.0.1 
      2020-05-12 – software update patch 1.0.2       2020-09-20 – software update patch 1.0.3   

In YAML, we can also type multiline strings in a single line utilizing the angle bracket > symbol. Please refer to the example below.

Multiline strings in a single line example:

report: >   
  Software updates fail
  since 2019-04-29.
  Currently investigating.

We have a report line as key followed by closing angular brackets > symbol. In the following line, we can see the multiline string that we want to see in a single line for the output.

Output:

▾ object {1}     report: Software updates fail since 2019-04-29. Currently             investigating.

 

Invalid Value and Empty Field

In YAML, we can set the value of a variable to be invalid. To do that, please take a look at the example below.

Invalid value example:

string1: null 
string2: ~ string3:  

Output:

string1: null
string2: nullstring3: null

Unlike an invalid value, an empty field is a value but empty. To write an empty field, you can use single quotes ”, please take a look at the example below.

Empty field example:

string4: ”

Output:

string4:
%d bloggers like this: