Trusted answers to developer questions

What is block style in YAML?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

Overview

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.

Examples

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
...
A "hello world" YAML document

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 Smith
age: 25
job: programmer
- name: Jane Doe
age: 30
job: manager
...
Block Style YAML for a more complex data structure

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.

Conclusion

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.

RELATED TAGS

yaml
Did you find this helpful?