This tutorial explains How to represent different array data structures in YAML content formats.

  • Array of Scalar Items
  • Nested Array
  • Array of objects
  • String array
  • Object with values as an arrays
  • empty array and objects
  • sequence of elements
  • Dictionary of elements

YAML Array of scalar items

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

Here is a syntax for yaml arrays.

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

Another way to rewrite in a single line, using square brackets syntax

Multiple values are represented and separated by commas enclosed in square brackets.

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

A single line is not compulsory, but you can wrap in multiple lines as given below 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"
  ]
}

YAML Documents of Arrays without keys

Another way, without a key, arrays can be represented in a 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 Objects

Objects contain multiple key and value pairs. An array of objects contains an object list.

Here is a code for the YAML array of objects example

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

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

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

YAML Nested Arrays Examples

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

YAML nested arrays can be represented in multiple ways.

  • use indentation syntax as below.
employees:
    -
        id: 213
        name: franc
        others:
            - { department: sales, did: 1}
            - { salary: 5000}
            - { address: USA, pincode: 97845 }

equivalent JSON is

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

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 a 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 with mixed types example as follows

floatvalues: [
  1.3,
  2.2,
  3,
  4
]

Yaml objects with arrays

The object contains key and value pairs. Value can be scalar or an array of scalar items.

author: Franc
database:
  driver: com.mysql.jdbc.Driver
  port: 3306
  dbname: students
  username: root
  password: root
support:
  - mysql
  - MongoDB
  - Postgres

Equivalent JSOn is

{
  "author": "Franc",
  "database": {
    "driver": "com.mysql.jdbc.Driver",
    "port": 3306,
    "dbname": "students",
    "username": "root",
    "password": "root"
  },
  "support": [
    "mysql",
    "MongoDB",
    "Postgres"
  ]
}

dictionary in yaml example

the dictionary contains keys and values.

In Yaml Dictionary can be represented in two syntaxes.

  • Flow mapping
  • Block mapping

Flow mapping dictionary:

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

Here is a syntax for Dictionary Flow Mapping

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

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

Following is an example of 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 is declared below.

myvalue:""

Another way

myvalue:[]