Skip to content
DeveloperMemos

Creating Arrays and Collections with YAML

YAML, Arrays, Collections2 min read

YAML, short for "YAML Ain't Markup Language," is a human-readable data serialization format that is commonly used for configuration files and, more recently, for defining structured data. Its simplicity and readability make it an excellent choice for defining arrays and collections within a variety of contexts. In this article, we'll explore how to create arrays and collections using YAML, delving into its syntax and providing examples to illustrate its usage.

Understanding YAML

Before we delve into creating arrays and collections, let's take a moment to understand the basics of YAML. YAML uses indentation to represent the hierarchical structure of data, making it similar to languages like Python. It also leverages key-value pairs and supports various data types, including strings, numbers, booleans, arrays, and maps. Collectively, these features make YAML both versatile and easy to use.

Creating Arrays in YAML

To define an array in YAML, you simply need to list the elements with a leading dash followed by a space. For instance, if we wanted to define a simple array of fruits, it would look like this:

1fruits:
2 - apple
3 - orange
4 - banana

In this example, "fruits" is the key, and the list of fruits following the key represents the array. The indentation signifies their association with the "fruits" key.

Defining Collections in YAML

Collections in YAML allow you to group related data under a single key. These collections can contain arrays, other collections, or a combination of both. Let's consider an example where we define a collection of employees, each associated with their respective details:

1employees:
2 - name: John Doe
3 role: Developer
4 years_of_service: 5
5 - name: Jane Smith
6 role: Designer
7 years_of_service: 3

In this instance, "employees" serves as the key for our collection. Each entry beneath "employees" represents a separate map containing details about an individual employee. This demonstrates how YAML enables the construction of structured collections with ease.

Advanced Array and Collection Usage

YAML's flexibility allows for more complex representations of arrays and collections. You can nest arrays within arrays, create dictionaries of arrays, or mix arrays and maps to suit your specific needs. Below is an example demonstrating a mix of these structures:

1company:
2 name: Acme Corporation
3 location: United States
4 employees:
5 - name: John Doe
6 role: Developer
7 - name: Jane Smith
8 role: Designer
9 products:
10 - Product A
11 - Product B
12 - Product C

In this advanced example, we've defined a map under the "company" key, which contains the company's name and location. Additionally, we've structured "employees" as a collection and "products" as an array.

In Closing

YAML's straightforward syntax and versatility make it a powerful tool for defining arrays and collections within your projects. Whether you're managing configurations, structuring data for applications, or defining test fixtures, YAML provides an intuitive and readable solution. By mastering its array and collection syntax, you can effectively organize and represent your data in a clear and concise manner.