A Guide to YAML Arrays

Authors

An array in YAML is a collection of values, where each value can be of any data type, such as strings, numbers, objects, or other arrays.

In YAML, arrays are represented as a series of values enclosed in square brackets [ ]and separated by commas.

Here's an example of a YAML array:

fruits:
  - apple
  - orange
  - banana
  

In this example, fruits is an array with three elements: apple, orange, and banana.

Each element is preceded by a hyphen - to indicate that it is an item in the array.

Array Of Objects In YAML

You can also have arrays of objects in YAML.

In that case, each item in the array is represented as a key-value pair, where the key is the name of the object, and the value is a YAML object containing the object's properties.

Here's an example of an array of objects in YAML:

people:
  - name: John
    age: 30
  - name: Jane
    age: 25

In this example, people is an array of two objects, where each object has a name and an age property.

Nested Arrays In YAML

You can also have nested arrays in YAML.

In that case, an array is simply an item in another array.

Here's an example of a nested array in YAML:

menu:
  - section: Starters
    items:
      - name: Salad
        price: 5.99
      - name: Soup
        price: 3.99
  - section: Main Courses
    items:
      - name: Steak
        price: 19.99
      - name: Fish and Chips
        price: 14.99

In this example, menu is an array of two objects, where each object represents a section of the menu.

Each section has an items property, which is an array of objects representing the items in that section.

Summary

In summary, YAML arrays are a way to represent collections of values, objects, or other arrays in YAML.

To create an array in YAML, enclose a list of values in square brackets and separate them with commas.

To create an array of objects, use key-value pairs to define each object, and enclose the objects in an array.

Finally, to create nested arrays, include an array as an item in another array.

TrackingJoy