Bren
Bren

Reputation: 2206

How to merge YAML anchor as a list item and override a value

Using a yaml configuration as below, I am trying to use the default_car as one of the items in a list and override the color attribute.

But there is a parse error stating: did not find expected key while parsing a block mapping

default_car: &default_car
  brand: xxx
  model: xxx
  color: red

cars_sold:
  - <<: *default_car
      color: blue

cars_available:
  - <<: *default_car
      color: yellow

Is it possible to achieve something like this ? If I am approaching this wrong, what would be the best way to not to repeat the default car mapping again and again ?

Upvotes: 6

Views: 1986

Answers (1)

NobleKangaroo
NobleKangaroo

Reputation: 61

For the sake of answering an old, unanswered question to help future searchers, it seems as though you simply need to remove erroneous additional spaces before the color overrides.

default_car: &default_car
  brand: xxx
  model: xxx
  color: red

cars_sold:
  - <<: *default_car
    color: blue

cars_available:
  - <<: *default_car
    color: yellow

Test it with https://jsonformatter.org/yaml-validator or https://yamlchecker.com/ to confirm this.

Upvotes: 6

Related Questions