8

I have the following in YAML:

key1
  key2: "value"

key1
  key2
    key3: "value2"

Get exception duplicate key key1.

Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing MappingNode

Tried various combinations but was unable to parse it correctly.

Saikat
  • 14,222
  • 20
  • 104
  • 125

4 Answers4

11

Your YAML is syntactically invalid, but I am assuming it actually looks like this:

key1:
  key2: "value"

key1:
  key2:
    key3: "value2"

Your error is that key1 is used two times as mapping key in the root node. This is illegal as per YAML spec:

The content of a mapping node is an unordered set of key: value node pairs, with the restriction that each of the keys is unique.

The solution is to make all keys of the same mapping unique:

key11:
  key2: "value"

key12:
  key2:
    key3: "value2"
flyx
  • 35,506
  • 7
  • 89
  • 126
10

I too faced the same issue. Then it struck on me! The answer is simple. From

mapping:
  refresh:
    schedule:
      frequency:
        milli: 86400000
mapping:
  refresh:
    schedule:
      initial:
        delay:
          ms: 30000

to

mapping:
  refresh:
    schedule:
      frequency:
        milli: 86400000
      initial:
        delay:
          ms: 30000
sivakadi
  • 181
  • 1
  • 4
0

so below simple solution worked for me. Basically, in the first scenario 'server' keyword came as a separate structure in the 2d scenario 'server' keyword came as child structure. I simply did a small indentation and it worked.

Before :->

server: port: 8761

eureka: client: registerWithEureka: false fetchRegistry: false

server : waitTimeInMsWhenSyncEmpty: false


After :-> server: port: 8761

eureka: client: registerWithEureka: false fetchRegistry: false server : waitTimeInMsWhenSyncEmpty: false

-3

You may fix it like this:

key1
  key2: "value"
  key2.key3: "value2"
ZXW
  • 9