Yaml syntax to create this array

5.9k Views Asked by At

Given the following simple yaml data,

foo: 1
bar:
  - one
  - two

if I want to create an array of the exact same data structure, what's the correct way?

I've tried

first:
  foo: 1
  bar:
    - one
    - two
    - three
second:
  foo: 2
  bar:
    - one1
    - two2
    - three3

or,

- foo: 1
  bar:
    - one
    - two
    - three
- foo: 2
  bar:
    - one1
    - two2
    - three3

and also,

- first:
    foo: 1
    bar:
      - one
      - two
      - three
- second:
    foo: 2
    bar:
      - one1
      - two2
      - three3

but none seems to be the correct way. Any help? Thanks!

1

There are 1 best solutions below

2
On BEST ANSWER

I think you're after either this:

- foo: 1
  bar:
    - one
    - two
    - three
- foo: 2
  bar:
    - one1
    - two2
    - three3

Which gives you this structure:

[
  {
    "foo": 1, 
    "bar": [
      "one", 
      "two", 
      "three"
    ]
  }, 
  {
    "foo": 2, 
    "bar": [
      "one1", 
      "two2", 
      "three3"
    ]
  }
]

Or this if the 'first' and 'second' labels are important to you:

first:
  foo: 1
  bar:
    - one
    - two
    - three
second:
  foo: 2
  bar:
    - one1
    - two2
    - three3

Which gives you a dictionary/associative array:

{
  "second": {
    "foo": 2, 
    "bar": [
      "one1", 
      "two2", 
      "three3"
    ]
  }, 
  "first": {
    "foo": 1, 
    "bar": [
      "one", 
      "two", 
      "three"
    ]
  }
}