YAML - Array, collection

This tutorial covers YAML tutorials, examples , Array list,Objects,strings,numbers,indented styles, dictionaries.

YAML Array list

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

Here is a syntax

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

This also represented in 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

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

Arrays of Arrays

Multidimensional Arrays or nested arrays 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
        }
      ]
    }
  ]
}

Arrays of Objects

Objects contains multiple key and value pairs. Array of objects contains list of objects Here is an json representation

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

This can be represented in YAML as

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

Arrays of String

Strings can be represented with double or single quotes Here is an example key with array of strings

numbers: [
  one,
  two,
  three,
  four
]

Here is an example array of strings without key

[
  one,
  two,
  three,
  four
]

Arrays of numbers

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

numbers: [
  1,
  2,
  3,
  4
]

Array of Numbers with floating values with 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"
  }
}