What is a Yaml Map?
A YAML map is a fundamental data structure used to represent hierarchical relationships of keys and values.
At its core, a YAML map serves as a collection where data is organized into distinct pairs, each consisting of a unique key and its associated value. This structure is ideal for representing objects, records, or configurations where you need to associate specific properties (keys) with their corresponding data (values). It provides a clear and readable way to organize complex information.
How Maps are Represented in YAML
In YAML, maps are typically represented by providing indented key-value pairs. The syntax for a key-value pair is the key, followed by a colon (:
), a space, and then the value.
# A simple YAML map
key_name: value_data
another_key: some_other_value
A key feature that enables hierarchy is that the value associated with a key can itself be another map. This is how you create nested structures, allowing you to represent increasingly complex and deep data relationships.
# A YAML map with nesting
user_profile:
name: Jane Doe
age: 28
contact:
email: [email protected]
phone: 123-456-7890
In this example, user_profile
is a key whose value is a map containing name
, age
, and contact
. The contact
key's value is yet another nested map.
Flexibility: Nesting and Combining Structures
As stated in the reference, YAML maps offer significant flexibility. They can be deeply nested to model intricate hierarchies. Importantly, maps can be seamlessly combined with other YAML structures, such as arrays (lists). This ability to mix and match maps and arrays allows for the representation of virtually any data structure.
Consider this example showing a combination of maps and arrays:
application_config:
environment: development
ports: # A key whose value is an array
- 80
- 443
database: # A key whose value is a nested map
type: mysql
host: db.example.com
credentials:
username: admin
password: password123
features: # A key whose value is an array of maps
- name: user_authentication
enabled: true
- name: logging
level: info
This demonstrates a main map (application_config
) containing simple key-value pairs, an array of simple values (ports
), a nested map (database
), and an array where each item is itself a map (features
). This versatility makes YAML maps a powerful tool for defining configurations and data structures.