1

When loading YAML anchors in node.js (v4.8.7) using the js-yaml 3.10.0 package (there's a nice example of using it here) I get the following error:

"cannot merge mappings; the provided source object is unacceptable"

For instance in my input yaml file I have something like the following as my anchor:

defaultEd: &defaultEd
  - 'Pennsylvania College of Technology AS'
  - 'Pennsylvania College of Technology BS'

And where the anchor is referenced in my input yaml file I have the following:

...
education:
  <<: *defaultEd
qs:
  - 'Reading'
  - 'Writing'
...

I'm hoping to accomplish the following in my output:

education:
  - 'Pennsylvania College of Technology AS'
  - 'Pennsylvania College of Technology BS'

The error is displayed something like this:

{ [YAMLException: cannot merge mappings; the provided source object is unacceptable at line 21, column 1:
    qs:
    ^]
  name: 'YAMLException',
  reason: 'cannot merge mappings; the provided source object is unacceptable',
  mark: 
   Mark {
     name: null,
     buffer: 'defaultEd: &defaultEd\n  - \'Pennsylvania College of Technology AS\'\n  - \'Pennsylvania College of Technology BS..
 <<: *defaultEd\nqs:\n  - \'Reading\'\n  - \'Writing\'\n  - \'Rithmatick\'\nexperience:\n  - {posName: \'Database Analyst / Net Tech\', companyName: \'Choices People Supporting People\'}\n\u0000',
     position: 435,
     line: 20,
     column: 0 },
  message: 'cannot merge mappings; the provided source object is unacceptable at line 21, column 1:\n    qs:\n    ^' }
Error View file does not exist: someTest.yml
leeand00
  • 25,510
  • 39
  • 140
  • 297
  • 1
    In this case there's no need to merge mappings. You can just use `education: *defaultEd`. I guess js-yaml forbids merge when there is nothing to merge. – tinita Jan 09 '18 at 15:46
  • Actually, I just saw, that the anchor `defaultEd` isn't a mapping. so there is no way to merge it. Can you specify what kind of data you expect to be in `education`? Probably my suggestion to use `education: *defaultEd` is still the right one. – tinita Jan 09 '18 at 16:36
  • @tinita Yeah that did it, thank you. I wasn't aware of what a mapping is, but I'm guessing that a mapping has something to do with it being a layered object of some sort? – leeand00 Jan 09 '18 at 19:27
  • @tinita You can add an answer if you want, I'll select it as the right one. – leeand00 Jan 10 '18 at 04:22

1 Answers1

5

You probably misunderstood / mixed up the usage of aliases and the merge key <<.

Any node in YAML can have an anchor attached:

---
mapping: &map
  a: 1
  b: 2
sequence: &seq
  - a
  - b
scalar: &scalar foo
mapping-alias: *map
sequence-alias: *seq
scalar-alias: *scalar

The merge key <<, which is not part of the YAML spec itself, is supported by some processors. It lets you merge an aliased mapping into another mapping.

defaults: &defaults
  a: 1
  b: 2
# .....
some-mapping:
  <<: *defaults
  c: 3

See http://yaml.org/type/merge.html

A mapping in YAML is usually called a dictionary, associative-array, hash or sometimes object (Javascript) in programming languages.

In your case, you probably just want:

education: *defaultEd
dreftymac
  • 31,404
  • 26
  • 119
  • 182
tinita
  • 3,987
  • 1
  • 21
  • 23