Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an array of unnamed objects in yaml?

Tags:

yaml

In yaml, how can I create a collection of objects without naming them?

servers:
  ip:
  port:
  login:
  password:

I want something like this:

servers:
  - server:
      ip:
      port:
      login:
      password:

  - server:
      ip:
      port:
      login:
      password:

  - server:
      ip:
      port:
      login:
      password:
like image 623
Alan Coromano Avatar asked Dec 15 '15 05:12

Alan Coromano


People also ask

How do you create an array of objects in YAML?

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. In a single line, write the same thing above using 'square brackets syntax.

Does YAML support arrays?

YAML supports all essential data types, including nulls, numbers, strings, arrays, and maps.

What is object in YAML?

YAML has two top-level elements, an object (which consists of key-value pairs) and an array (which consists of an ordered set of values). Keys can only be strings, while values can be one of the following types: object: a collection of key-value pairs.


1 Answers

YAML sequence sign (-) is just a delimiter, so it allows to enumerate unnamed objects:

servers:
  - ip:
    port:
    login:
    password:

  - ip:
    port:
    login:
    password:

  - ip:
    port:
    login:
    password:

You may also leave - at its own line, as it is shown in Example 2.4 in YAML spec:

servers:
  -
    ip:
    port:
    login:
    password:
like image 164
Tsyvarev Avatar answered Sep 29 '22 00:09

Tsyvarev