By using below example of YAML file content will explain about YAML syntax so that easily understandable.
Example :
---
# My personal record
name: Saurabh Kumar Gupta
Title: Sr. Project Lead
skill: JAVA/J2EE
employed: True
domains:
- Telecom
- Finance
- Banking
- Healthcare
languages:
ELK: Medium
JAVA: Expertize
Scripting: Comfortable
education: |
MCA
B.Sc
Diploma
...
Comments
YAML documents comments start with #.
Example : #My personal record
Documents
YAML documents start with (- – -) and ends with (. . .) (optional)
Example : as above YAML content having only one document.
List
YAML all members of a list are lines beginning at the same indentation level starting with a “-” (a dash and a space).
Example: as above YAML content having domains list as below
domains:
- Telecom
- Finance
- Banking
- Healthcare
Dictionary
A YAML dictionary is represented in a simple key: value form (the colon must be followed by a space).
Example : as above YAML content having dictionary as languages.
languages:
ELK: Medium
JAVA: Expertize
Scripting: Comfortable
List and Dictionary Together
---
# Employee records
- Saurabh
name: Saurabh Kumar Gupta
Title: Sr. Project Lead
skill: JAVA/J2EE
employed: True
domains:
- Telecom
- Finance
- Banking
- Healthcare
- Gaurav
name: Gaurav Kumar Gupta
Title: Project Lead
skill: ELK
employed: True
domains:
- Telecom
- Finance
Boolean
you can also specify a boolean value (true/false) in several forms.
employed: yes
employed: no
employed: True
employed: false
employed: TRUE
If need to use Boolean value as String Literal use as below
employed: "yes"
employed: "no"
employed: "True"
employed: "false"
employed: "TRUE"
Multiline Value
YAML values can span multiple lines using two ways by | or >.
- “Literal Block Scalar” | will include the newlines and any trailing spaces.
- “Folded Block Scalar” > will fold newlines to spaces;
It’s used to make what would otherwise be a very long line easier to read and edit. In either case the indentation will be ignored.
education: |
MCA
B.Sc
Diploma
and
education: '>
MCA
B.Sc
Diploma'
Special Characters
YAML allowed anything to put unquoted but there are some special cases where need to show value with quote.
Character (:) represents as mapping and Character (#) for comments. if these characters are occurs with space in any text value will use with (‘ ‘ or ” “) quotes.
Example :
description: 'you can write your code here: so that we can copy.'
or
description: "you can write your code here: so that we can copy."
The difference between single quotes and double quotes is that in double quotes you can use escapes.
description: "it\'s time to go home."
Variables
YAML uses “{{ var }}” for variables. If a any value after a colon starts with a “{” (curly bracket), YAML will think it is a dictionary, so you must quote it, like so:
Example :
log_file :"{{ LOG_DIR}}\\apps\\app_logs-*.log"
Note:
These reserved or special characters (‘ ” :[] {} > | * & ! % # ` @ ,) can not be used as first character of unquoted scalar. Only allowed characters are ( ? : ) on beginning of string if a non-space character follows. Better always use quote for these type of scalar values.
You must be logged in to post a comment.