Hello, everyone! I am Goutham, and in this blog I would like to explain intermediate YAML syntax. I have already written a blog on the basics of the YAML syntax and its equivalent JSON syntax.
Multiple documents
Using 3 hyphens --- we can write multiple YAML documents in a single file .
---
name: Apple
color: Red
---
name: mang fileo
color: Yellow
... # 3 dots are used to end YAML without starting new one ...
Anchors and Alias
While writing large YAML files, most of the stuff may repeat, which makes the file large and difficult to understand. What if, just like in programming languages, storing data in variables and using it where ever we want was present in YAML as well?
This is where Alias comes in.
name: Apple
properties:
color: red
cost: 30Rs
name: Tomato
properties:
color: red
cost: 30Rs
name: Cherry
properties:
color: red
cost: 30Rs
name: Apple
properties: &store
color: red
cost: 30Rs
name: Tomato
properties: *store
name: Cherry
properties: *store
What if we want to manipulate some of the values instead of blind replicating?
name: Apple
properties: &store
color: red
cost: 30Rs
name: Tomato
properties:
<<: *store # replicating everything in properties
cost: 20Rs # value in cost key will be changed now to 20Rs
name: Cherry
properties:
<<: *store
cost: 40Rs
DataTypes
Datatypes are used when we want to explicitly mention the type of value we want to store.
Integer: !!int 78
Float: !!float 89.98
Nullvalue: !!Null # Here anything among Null,NULL,null, ~ can be used
binary: !!int 0b11 # stores 3
hexadecimal: !!int 0x11 # stores 17
octal: !!int 011 # stores 9
boolean: !!bool 1 # we can store 0 ,1 ,true,false YAML can convert it.
string: !!str 9 # stores 9 as a string
If we want to use pairs which always occur together then..
apple: !!pairs
- color: red ## color,red always occur together
An equivalent JSON would be
{
"apple": [
[
"color",
"red"
]
]
}
We can even do this as well.
apple:
-
- color
- red
This also provides the same JSON output.
Thank you so much for reading my blog on YAML2. If you find any mistakes please let me know.
Thank you, Mariusz Michałowski , for helping me to write a blog on YAML.
This is Mariusz article on YAML have a look .
https://spacelift.io/blog/yaml
Have a Great day 🎉🎉🎉.