Block style is a way of formatting YAML documents that are typically used to represent hierarchical data structures. In block style, each element of the document is completely contained within its own block element. This allows for more flexibility and control when working with large or complex data structures, as individual elements can be easily referenced, modified, or removed without affecting the rest of the document.
One common way to format a YAML document in block style is by starting with a top-level root element and then indenting all child elements from that level. For example, here is a simple "hello world" program represented using this convention:
---hello: world...
In the above code, ---
denotes the start of a new document, whereas ...
denotes the end of the document.
To represent nested data structures, simply continue indenting child elements from their parent element. For example, here is a more complex data structure that contains a list of user information:
---users:- name: John Smithage: 25job: programmer- name: Jane Doeage: 30job: manager...
Block style allows us to represent hierarchical data structures in a simple and straightforward way, making it an ideal choice for working with large or complex data.
We covered what block style in YAML is, how it is used to represent hierarchical data structures, and some of the different formatting conventions that are commonly used. We also looked at a few code examples to help illustrate how this formatting style can be used in practice. Whether you are working with simple or complex data, block style offers a powerful and flexible way.
To learn more about the syntax, data types, and various key concepts related to the YAML language, check this course at educative: Introduction to YAML.
Unlock your potential: YAML basics series, all in one place!
To continue your exploration of YAML basics, check out our series of Answers below:
What is a YAML file?
Understand what YAML files are and the different styles they use.
What is block style in YAML?
Learn the block style format for structuring data in YAML.
What is flow style in YAML?
Discover how flow style differs from block style and when to use it.
How to represent different basic data types in YAML
Explore how YAML handles various basic data types, such as strings, integers, and booleans.
How to represent strings in YAML
Discover how YAML handles string values and different formatting options.
How to represent arrays in YAML
Explore how YAML represents arrays, with each element preceded by a hyphen (-
).
How to represent key-value pairs in YAML
Learn how to effectively represent key-value pairs within YAML syntax.
How to represent maps in YAML
Master how to structure key-value pairs as maps in YAML.
How to represent sequence in YAML?
Understand how to represent sequences in YAML files.
How to represent dictionaries in YAML
Explore how to represent complex data structures like dictionaries in YAML.
How to represent null values in YAML
Learn how to properly represent null or missing values in YAML.
How to write comments in YAML
Learn how to add comments in YAML to improve readability.
What are the advantages of using YAML over other data formats?
Understand why YAML is often preferred over JSON and XML for data serialization.