YAML Multi-Line Arrays

Yaml

Yaml Problem Overview


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?

Yaml Solutions


Solution 1 - Yaml

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']

Solution 2 - Yaml

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).

Solution 3 - Yaml

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

key:
  - [ 'value11', 'value12', 'value13' ]
  - [ 'value21', 'value22', 'value23' ]

Solution 4 - Yaml

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.

Solution 5 - Yaml

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJimboView Question on Stackoverflow
Solution 1 - YamlSteve BennettView Answer on Stackoverflow
Solution 2 - YamlIwan B.View Answer on Stackoverflow
Solution 3 - YamlViniciusView Answer on Stackoverflow
Solution 4 - YamlYeeView Answer on Stackoverflow
Solution 5 - YamlHarsimranjit Singh KlerView Answer on Stackoverflow