user5591691
user5591691

Reputation:

Duplicate key in YAML configuaration file

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.

Upvotes: 8

Views: 39099

Answers (4)

akshay khirolkar
akshay khirolkar

Reputation: 1

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

Upvotes: -1

ZXW
ZXW

Reputation: 9

You may fix it like this:

key1
  key2: "value"
  key2.key3: "value2"

Upvotes: -3

sivakadi
sivakadi

Reputation: 201

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

Upvotes: 11

flyx
flyx

Reputation: 39638

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"

Upvotes: 13

Related Questions