What is a YAML file?
YAML file consists of a language YAML (YAML Ain’t Markup Language) which is a Unicode based data-serialization language; used for configuration files, internet messaging, object persistence, etc. YAML uses the .yaml extension for its files. Its syntax is independent of a specific programming language. Basically, the YAML is designed for human interaction and to work well with modern programming languages. Support for serializing arbitrary native data structures increased the readability of the YAML files, but it has made the parsing and file generation process complicated a little.
Brief History
YAML was first proposed in 2001 and was developed by Clark Evans, Ingy döt Net, and Oren Ben-Kiki. YAML was first said to mean “Yet Another Markup Language” to indicate its purpose as a markup language. It was later repurposed as “YAML Aint Markup Language” to indicate its purpose as data-oriented.
YAML File Format
YAML file consists of the following data types
- Scalars: Scalars are values like Strings, Integers, Booleans, etc.
- Sequences: Sequences are lists with each item starting with a hyphen (-). Lists can also be nested.
- Mappings: Mapping gives the ability to list keys with values.
Syntax
Whitespace: Whitespace indentation is used to indicate nesting and overall structure.
name: John Smith contact: home: 1012355532 office: 5002586256 address: street: | 123 Tornado Alley Suite 16 city: East Centerville state: KS
Comments: Comments are written beginning with the “#” symbol.
# This is a YAML Comment
Lists: Hyphen (-) is used to indicate list members with each member on a separate line. List members can also be enclosed in square brackets ([…]) with members separated by commas (,).
- A - B - C
[A, B, C]
Associative Array: An associative array is surrounded by curly brackets ({…}). The keys and values are separated by colon(:) and each pair is separated by comma (,).
{name: John Smith, age: 20}
Strings: String can be written with or without double-quotes (") or single-quotes (’).
Sample String "Sample String" 'Sample String'
Scalar Block content: Scalar content can be written in block notation by using the following:
- |: All live breaks are significant.
- >: Each line break is folded to space. It removes the leading whitespace for each line.
data: | YAML (YAML Ain't Markup Language) is a data-serialization language
data: ? YAML (YAML Ain't Markup Language) is a data-serialization language
Multiple Documents: Multiple documents are separated by three hyphens (—) in a single stream. Hyphens indicate the start of the document. Hyphens are also used to separate directives from document content. The end of the document is indicated by three dots (…).
--- Document 1 --- Document 2 ...
Type: To specify the type of value, double exclamation marks (!!) are used.
a: !!float 123 b: !!str 123
Tag: To assign a tag to a note, an ampersand (&) is used and to reference that node, an asterisk (*) is used.
name: John Smith bill-to: &id01 street: | 123 Tornado Alley Suite 16 city: East Centerville state: KS ship-to: *id01
Directives: YAML documents can be preceded by directives in a stream. Directives begin with a percent sign (%) followed by the name and then the parameters separated by spaces.
%YAML 1.2 --- Document content
YAML file example
Here you can see a docker yaml file example below:
topology:
database_node_name: docker_controller
docker_controller_node_name: docker_controller
self_service_portal_node_name: docker_controller
kvm_compute_node_names: kvm_compute1
docker_compute_node_names: docker_compute1
YAML vs JSON
Basically, both JSON and YAML are developed to provide a human-readable data interchange format. The YAML is realized as a superset of JSON format. It means that we can parse JSON using a YAML parser. Although the practical implementation of this theory is little tricky. Therefore, some basic differences between YAML and JSON are given below:
YAML | JSON |
---|---|
Complex and time consuming process of parsing Serialized data | Quickly and easily parse JSON serialized data with its simpler design |
Less community support | Larger community support and popularity |
Supports comments | Doesn’t support comments |
Ability to use reference of other data objects | Impossible to serialize complex structures with object references |
Hierarchy is denoted by using double space characters. Tab characters are not allowed | Objects and Arrays are denoted in braces and brackets. |
String quotes are optional but it supports single and double quotes. | Strings must be in double quotes. |
Root node can be any of the valid data types | Root node must either be an array or an object. |