Tag Archives: YAML Syntax

YAML Introduction

YAML(Ain’t Markup Language) is a human friendly , cross language, unicode based data serialization format designed for interaction with all programming languages.

File Name Extension : .yml or .yaml

Latest Release : 1.2

It is broadly use for programming:

  • Configuration Files
  • Internet Messaging
  • Object Persistence to data auditing
  • Also support for Unicode standards characters.

YAML allow data to show itself in a natural and meaningful way and provide a unique cleanness by minimizing the amount of structural characters.
For example: indentation may be used for structure, colons use to separate “mapping key: value” pairs, and dashes are used to create “bullet” lists for showing collections of data.

YAML is a superset of JSON : you can convert YAML to JSON by different API’s

YAML Goals

Below are main design goals for YAML :

  • YAML is easily readable and understandable by humans.
  • YAML is expressive and extensible.
  • YAML is easy to implement and use.
  • YAML matches the native data structures of agile languages.
  • YAML data is portable between programming languages.
  • YAML has a consistent model to support generic tools.
  • YAML supports one-pass processing.

YAML Documents Processing

A YAML document is parsed only when the iterator is invoked (lazy evaluation).

More

To know more about YAML Syntax, Configuration with Java and other supporting language, frameworks and tools, Sample configuration files and JSON and YAML conversion follow below YAML Tutorials and YAML related exceptions follow YAML Issues.

 

Advertisements

YAML Syntax

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.