Abbreviated Forms and Flow Collections

YAML Abbreviated Forms

YAML Abbreviated forms (flow style or also called inline format) helps us to simplify the YAML code. It represents the unordered collection pairs of key and value within the code. Therefore, the syntax can be more readable and brief.

Abbreviated Form in the List Format

Compared to the conventional block format, the flow style uses the bracket sign ([]) and separates the items using comma and space. For further explanation, take a look at the given example below.

Conventional Block Format

# Shopping list
vegetables:
   - Onion
   - Spinach
   - Celery
   - Carrot

Abbreviated form (flow collection)

--- # Shopping list
vegetables:[‘Onion’, ‘Spinach’, ‘Celery’, ‘Carrot’]

Abbreviated Form in Dictionary Format

It is also possible to create the abbreviated form of a dictionary. 

Dictionary formAbbreviated form
# Microbiologist recorddata:name: Anizspecialty: endophytesworkplace: western lab— # Microbiologist recorddata: {name: Aniz, specialty: endophytes, workplace: western lab}

YAML applies the key: value form to form its body. In the example given above, we can see the data represents the key. The value is represented by name: Aniz, specialty: endophytes, and workplace: western lab. To abbreviate the dictionary, surround the value with the “{}”. The block values can be separated by “,” and space.

We can also compose an abbreviated form of YAML body from a complex syntax (namely, nested value). Here is an example of a complex dictionary with nested values.

donna:
      job:  Department Head
      name:  Donna Frmntation
      skills:
       -  pgpr
       -  research
       -  laboratory management

Therefore we will get the following abbreviated form.

donna: [ job:  ‘Department Head’,  name: ‘Donna Fermentation’, 
skills:  [‘pgpr’, ‘research’, ‘laboratory management’] ]

Implicit and explicit key within the flow style

Single Pair Implicit Entries

Here we have an example of a single pair of implicit entries.

[endophytes: exist]
[xylanase: ⁰]

If we change the example above into the flow collection form, the result will be displayed like this.

[ [ { “endophytes”: “exist” } ], [ { “xylanase”: null } ] ]

The “⁰” in the example means an implicit symbol for a null value. While the null value itself side by side with xylanase conforming to the key: value principle. Same goes to the endophytes and exist.

Single Pair Explicit Entries

Take a look at a given example below. 

[? seed type: dicotyledonae]

After that, we will have the flow collection style or inline format from the very same syntax.

[ { “seed type”: “dicotyledonae” } ]

The explicit character used in the example above is the “?” indicator. It means a notation to a mapping key. The specified “?” makes the parsing unambiguous, therefore the syntax is identical to the general case.   

YAML allows us to combine both the implicit and explicit entries. Here is an example.

{  ? seed type: dicotyledonae,  endophytes: !!str⁰,}
{ “seed type”: “dicotyledonae”, “endophytes”: “ ”}

Flow styles in scalar

There are three kinds of flow styles that are supported in YAML’s scalar. They are double-quote, single-quoted, and unquoted. Each style has its own limitations. These styles will be briefly explained as follows.

Double-quote

Double-quote style is indicated by the “ ‘’ ” which surrounds the key and value. This style has the capability of expressing the non-printable characters by the usage of “\”. Here we have some examples for the double-quote style.

Double-quoted implicit keys

‘‘primes’’: [
     ‘‘factor of six’’: two,
]

The flow style result:

{ ‘‘primes’’: [ { ‘‘factor of six’’: ‘‘two’’ } ] }

Double-quoted line breaks

‘‘line
 the start,
      the middle, or \
         \ the end’’

The flow style result:

‘‘line the start, \nthe middle, \t \tor the end’’

Double-quoted lines

‘‘ First line
Second line
    Third line ’’

The flow style result:

‘‘ First line \nSecond line Third line ’’

Single-quoted

The “ ‘ “ indicator marks the single-quoted style specifically. This style allows the application of the “\” and “ ‘’ ” freely. However, this style has its limitations as well, such as repetitive characters within a scalar and the restricted possibility only to break a long single-quoted where a space character (be it space or tab) is surrounded by non-space characters. Check the example below.

‘ The blank‘ ’ space ’

Therefore, we will get this flow style.

“The blank’ space”

Unquoted (plain)

The unquoted or plain style does not have any indicators nor support printable character (escaping form). This style is the most readable one compared to the other styles, yet also the most sensitive when it comes to context. It does not support any indicators at the beginning of syntax, except the “:”, “?” and “-” characters only if they are followed by certain non-space characters. Otherwise it will be ambiguous. Check the given example below.

::this array
-23
String!

  From the syntax above, we will have this flow style:

[ “::this array”, -23, “String!” ]

Want to learn more?

Now that you’re an expert on YAML Abbreviated Forms and Flow Collections, check out the Language Features page for more YAML information.

Check out the YAML website for more information.

%d bloggers like this: