228

In YAML, you can easily create multi-line strings. However, I would like the ability to create a multi-line array (mainly for readibility within config files) using the | character.

A YAML array can be represented as: ['key1', 'key2', 'key3'].

A YAML sequence uses a dash followed by a space and then a string:

- String1
- String2
- String3

This would evaluate to: ['string1', 'string2', 'string3'].

A YAML mapping is an array of key and value pairs that we see all the time in YAML:

Key1: string1
Key2: string2
Key3: string3

This is all well and good, but I can't for the life of me see how to do a multi-line array. Something like this:

|
['string1', 'string2', 'string3']
['string4', 'string5', 'string6']

Short of creating multiple array mappings in YAML and merging them in my programming language of choice, is there any way to achieve multi-line arrays, maybe with { } like Python has but in YAML?

Community
  • 1
  • 1
Jimbo
  • 25,790
  • 15
  • 86
  • 131
  • honestly, whats a multi-line array & when should it be meaningful at all? Rather than just the looks of your code – OK999 Oct 22 '17 at 15:32
  • 3
    Having to scroll across the screen horizontally to see all the options rather than see them all one one screen at once. – Jimbo Oct 22 '17 at 16:53
  • 3
    Multi-line may make diffs much more readable, since every item added or removed corresponds to 1 line added or removed. – XedinUnknown Jun 15 '21 at 07:34
  • @XedinUnknown Good technical argument and reasoning - version control usage is impacted less with future changes. Thanks for sharing. – Jimbo Jun 16 '21 at 11:16

5 Answers5

410

A YAML sequence is an array. So this is the right way to express it:

key:
  - string1
  - string2      
  - string3
  - string4
  - string5
  - string6

That's identical in meaning to:

key: ['string1', 'string2', 'string3', 'string4', 'string5', 'string6']

It's also legal to split a single-line array over several lines:

key: ['string1', 'string2', 'string3', 
  'string4', 'string5', 
  'string6']

and even have multi-line strings in single-line arrays:

key: ['string1', 'long
  string', 'string3', 'string4', 'string5', 'string6']
Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
  • 2
    is it an issue if the strings start with a - (e.g. options passed in the command line)? do I then have to quote? ` - "-myarg"`? – ekkis Mar 16 '17 at 23:31
  • 3
    That depends totally on whatever program you're using to parse the YAML. This question is just about the YAML specification. – Steve Bennett Mar 17 '17 at 05:05
  • i did not know that YAML could handle brackets like that. thank you :-) – BenKoshy Jun 07 '21 at 04:03
88

have you tried this?

-
  name: Jack
  age: 32
-
  name: Claudia
  age: 25

I get this: [{"name"=>"Jack", "age"=>32}, {"name"=>"Claudia", "age"=>25}] (I use the YAML Ruby class).

Iwan B.
  • 3,982
  • 2
  • 27
  • 18
17

If what you are needing is an array of arrays, you can do this way:

key:
  - [ 'value11', 'value12', 'value13' ]
  - [ 'value21', 'value22', 'value23' ]
Vinicius
  • 432
  • 4
  • 8
  • 1
    Thanks Vinicius, but the question explicitly stated: ".I would like the ability to create a multi-line array", not multiple lines of multiple arrays. – Jimbo Jun 02 '20 at 07:04
  • 10
    didn't realize the question wasn't exactly mine, but came for this - thanks @Vinicius – nisseknudsen Feb 17 '21 at 11:58
10

The following would work:

myarray: [
  String1, String2, String3,
  String4, String5, String5, String7
]

I tested it using the snakeyaml implementation, I am not sure about other implementations though.

Yee
  • 861
  • 1
  • 8
  • 13
  • 2
    it worked with Puppet Enterprise 3.7 hiera yaml (Ruby yaml) one thing I find ruby yaml is quite sensitive too is the identation, and avoid using tabs. so example above should be like in two lines idented same number of spaces where first line is `myarray: [String1, String2, String3,` and second line is ` String4, String5, String5, String7 ] ` – Walid Jan 29 '15 at 08:50
5

The following works for me and its good from readability point of view when the number of array element values is small:

key: [string1, string2, string3, string4, string5, string6]

This has been tested to work with snakeyaml and ruamel.yaml.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Harsimranjit Singh Kler
  • 2,006
  • 1
  • 18
  • 21