YAML Array list

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

Here is a syntax for yaml arrays

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

In a single line, write the same thing above using ‘square brackets syntax.’

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

and also equivalent representation using multiple lines

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

The json representation is as below.

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

and without a 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 the above arrays

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

YAML Arrays of Arrays

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

use indentation syntax as below.

employees:
    - 
        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 contain multiple key and value pairs. An array of objects contains an object list.

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 the array of objects example as follows

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

Arrays of String in YAML

Strings contain a group of characters. It is a scalar type in yaml.

Keys in the string array are optional in YAML. Here is an example of an array of strings with keys.

numbers: [
  one,
  two,
  three,
  four
]

Here is an example array of strings without key

[
  one,
  two,
  three,
  four
]

Arrays of numbers in YAML

Numbers are predefined scalar types in yaml. It accepts integer or floating numbers.

An array of numbers or mixed numbers also can be represented as follows

numbers: [
  1,
  2,
  3,
  4
]

An array of Numbers with floating values combined mixed types example as follows

floatvalues: [
  1.3,
  2.2,
  3,
  4
]

dictionary in yaml

dictionary contain keys and values.

In Yaml Dictionary can be represented in two sytnaxes

  • Flow mapping
  • Block mapping

Flow mapping dictionary:

In this, key and value pair are separated by comma and entire pairs are enclosed in {} characters.

Here is an syntax for Dictionary Flow Mapping

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

Block mapping Dictionary: In this key and value pairs represented using colon : Syntax

Following is an example for dictionary Block mapping 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 Dictionary in yaml

{} denotes an empty dictionary for flow mapping syntax in yaml. Here is an example

employee: {}

null denotes an empty dictionary for block mapping syntax in yaml. Here is an example

employee: null

How do you create an empty array in yaml

[] denotes an empty array in yaml.

the empty array defined using a key is a variable of type string,value is an empty array like below

array:[]

create an empty sequence

An empty sequence declared with below

myvalue:""

Another way

myvalue:[]