YAML Array list

Array is a group of similar values with single name. In YAML, Array represented with single key mapped to multiple values. Each value is started with hyphen - symbol followed by space.

Here is a syntax for yaml arrays

key1:
  - value1
  - value2
  - value3
  - value4
  - value5

The same above can alternatively represented using square brackets syntax in single line

key1: [value1,value2,value3,value4,value5]

and also equivalent representation using multiple lines

key1: [value1,value2
  value3,value4,value5]

The json representation as follows

{
  "key1": [
    "value1",
    "value2",
    "value3",
    "value4",
    "value5"
  ]
} 

and also without key, arrays can be represented as in single line using square brackets

 [one, two, three, four]

or can also be declared in a single line

- one
- two
- three
- four

Equivalent JSON is for above arrays

 [
  "one",
  "two",
  "three",
  "four"
]

YAML Arrays of Arrays

Arrays of Arrays are also called multi dimensional arrays or nested arrays.

These can be represented using indentation syntax

emloyees:
    - 
        id: 213
        name: franc
        others:
            - { department: sales, did: 1} 
            - { salary: 5000}
            - { address: USA, pincode: 97845 }

equivalent JSON is

{
  "emloyees": [
    {
      "id": 213,
      "name": "franc",
      "others": [
        {
          "department": "sales",
          "did": 1
        },
        {
          "salary": 5000
        },
        {
          "address": "USA",
          "pincode": 97845
        }
      ]
    }
  ]
}

YAML Arrays of Objects

Objects contains multiple key and value pairs. Array of objects contains list of objects.

Here is an code for YAML array of objects example

one:
  - id: 1
    name: franc
  - id: 11
    name: Tom

The same above can be YAML equivalent of array of objects example as follows

{
  "one": [
    {
      "id": 1,
      "name": "franc"
    },
    {
      "id": 11,
      "name": "Tom"
    }
  ]
}

Arrays of String in YAML

Strings contains group of characters, It is an scalar type in yaml.

YAML string array can be represented with or without key.

Here is an example for array of strings with key.

numbers: [
  one,
  two,
  three,
  four
]

Here is an example array of strings without key

[
  one,
  two,
  three,
  four
]

Arrays of numbers in YAML

numbers in yaml can be integer or floating numbers and these are predefined scalar types.

Array of numbers or mixed numbers also can be represented as follows

numbers: [
  1,
  2,
  3,
  4
]

Array of Numbers with floating values combined mixed types example as follows

floatvalues: [
  1.3,
  2.2,
  3,
  4
]

dictionaries types

Dictionaries are key and values with specific keys. Following is an example for dictionary types of data

# dictionary
  mysqldatabase:
    hostname: localhost
    port: 3012
    username: root
    password: root

Equivalent JSON is

{
  "mysqldatabase": {
    "hostname": "localhost",
    "port": 3012,
    "username": "root",
    "password": "root"
  }
}

How do you create an empty array in yaml

[] denotes an empty array in yaml.

empty array defined using an key is an variable of type string and value is an empty array like below

array:[]

create an empty sequence

Empty sequence can be defined with below

myvalue:""

Another way

myvalue:[]