YAML Arrays example
Arrays are a group of fixed-size elements.
Array elements in the YAML
file contain separate lines, Also called multiline array strings.
In YAML, array syntax is
let’s declare an array in any programming languages
["admin", "finance", "hr"]
Above written with yaml sequence
also
---
- admin
- finance
- hr
Let’s declare the key and value as an array in YAML.
roles contain an array of admin, HR, and sales, finance
We have to use a sequence with key=roles, and the value is a multi-line array of strings.
Here is an example of the YAML
sequence that is the same as multi-line strings.
---
roles:
- admin
- finance
- hr
- sales
Also, this can be represented in multiple ways
"roles":["admin","finance","hr","sales"]
This is good as it is very easy to read and understand. or
"roles":["admin","finance","hr",
"sales"]
Array of arrays in yaml in multiline strings
Suppose you have a list of employee objects in json that contain an array of arrays.
{
"employees": [
[
"emp1",
"emp2",
"emp3"
],
[
"emp11",
"emp12",
"emp13"
]
]
}
YAML represents an array of arrays as follows
---
employees:
- - emp1
- emp2
- emp3
- - emp11
- emp12
- emp13