YAML Syntax - Mappings

YAML mappings are a way to represent associations between keys and values, much like dictionaries or hashes in many programming languages.

Mappings

In YAML, we use mappings (also known as hashes or dictionaries) to represent relationships between keys and values. A typical YAML file using mappings could be a configuration file for a simple application, looking something like this:

database:
  host: db.example.com
  username: admin
  password: secret

Here, we have a top-level key database and its corresponding value is another mapping - host, username, and password are the keys with their own values.

Inline and Block styles

Similar to Inline and Block sequences, Mappings allow us to write these in two styles as well:

Block Style: This is the traditional style you'll come across in most YAML documents. It's straightforward, with each key-value pair on its own new line, indented to represent hierarchy.

Inline Style: Now, for those of you who enjoy speed, YAML offers the inline style - all your data on a single line! This style uses curly braces {} and commas to denote different key-value pairs. Here's how the same example would look in inline style:

database: { host: db.example.com, username: admin, password: secret }

YAML mappings are great for making associations between keys and values. You can pick the style based on your requirements and the complexity of your data.

Previous Next

Written © 2024 Written Developer Tutorials and Posts.

𝕏