YAML is a human-readable data serialization standard that can be used in conjunction with all types of programming languages. It is often used to write configuration files.
YAML is like JSON and XML which are used for data serialization. It is written in plain text format.
Syntax Comparison between XML, JSON, and YAML
XML | JSON |
<customers> <customer> <name>Dhanik Lal Sahni</name> <email>salesforcecodex@gmail.com</email> </customer> </customers> | { customers: { name:”Dhanik Lal Sahni”, email:”salesforcecodex@gmail.com” } } |
YAML Syntax:
YAML is created with line separation and indentation. Space is very important in YAML configuration and it should be properly added. Extra space can cause a YAML validation issue.
Usage of YAML
YAML is being used in almost all DevOps tools for the configuration files. It is also used in creating Open API specifications. Mulesoft Anypoint studio uses this for creating API specifications.
YAML Syntax
YAML has to follow a few syntaxes to create the configuration file. Below are some important configuration elements.
1. Key Value Pair
We have to write each configuration in key-value pair. Almost all values are written in this way only. We have to follow the data type specification for writing values.
name: "dhanik lal sahni
post-date:2022-08-19
2. Comments
To write down comments in the configuration file we have to use #. We can include comments in any line.
# code comment here
3. List/Collection
dash (-) is used to create a list or collection in YAML. In the below example list of customers is created using dash (-).
customers:
- name:"Dhanik Lal Sahni"
email:"salesforcecodex@gmail.com"
or
customers:
- "Dhanik Lal Sahni"
- "salesforcecodex@gmail.com"
or
versions:[1.0,1.1,1.2]
4. Multi Line Strings
To create multiline string we have to use pipe (|).
address: |
Address1
Address2
City
State
ZipCode
We can use greater then sign (>) to use multi line string into single line
address1: >
This is single line even
though it is spread in multiline
5. Access Enviornmental Variable
We can access environmental variable using doller ($) symbol.
Enviornmental variable Declration
price: $price 100
Usage:
payment-proce:$price
6. Place Holder/ Dynamic Values
we should use double curly braces for ({{, }}) to create place holder for dynamic values.
address:
address1: {{.values.address.address1}}
address2: {{.values.address.address2}}
7. Multi Component section in file
We can use three dash (—) to create seprate section or component in single file
customers:
- name:"Dhanik Lal Sahni"
email:"salesforcecodex@gmail.com"
---
products:
- name:"Product 1"
price: 100
8. Reference Value
We can create reference variable using ampersand (&) and refer it using less than operator (<<).
Create Reference variable
country: &country
name: India
code : +91
Refer this reference variable
customers:
name: Dhanik
<<: *country
YAML Validator
Once the YAML configuration file is created. We should always validate it. There are many validators available online, you can use any one. You can use YAML Validator also.
Reference
Related Posts
Types Of Integration Patterns in Salesforce
2 comments
[…] What is YAML? […]
[…] What is YAML? […]