0

I'm working with a dropwizard app and one of the endpoints accepts JSON.

Currently, if a json object like this is passed as a POST or PUT request, using Postman for example, it holds onto the last values.

{
  "key", "value1",
  "key", "value2"
}

it gets deserialised into an object and then if a GET is performed, the response will be like this.

{
  "key", "value2"
}

Dropwizard seems to deal with this by overriding the map with latest values.

I was wondering if there is a way to basically prevent JSON with duplicate keys from being accepted as part of the PUT request, and just give error back to user.

Any help much appreciated. Thanks

Tunit
  • 1
  • 2
  • 1
    Your examples are not JSON but something that does not resemble anything (maybe arrays of strings but with incorrect wrappers). An object with duplicate keys is ambiguous and the parser is free to keep whatever value it wants (the first one, the last one or even an intermediate one, if there are more than two). If your application wants to accept multiple values for the same key then it should require an array for that key. – axiac Aug 11 '22 at 16:04
  • Thanks @axiac for the answer. I'm actually trying to prevent multiple values for same key, instead of the default of keeping the last one. – Tunit Aug 11 '22 at 16:09
  • Does this answer your question? [Jackson detection of duplicate JSON POJO properties and Map keys](https://stackoverflow.com/questions/23781637/jackson-detection-of-duplicate-json-pojo-properties-and-map-keys) – David Conrad Aug 11 '22 at 16:12
  • The key detail from [this answer to that question](https://stackoverflow.com/a/37154167/636009): `objectMapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);` – David Conrad Aug 11 '22 at 16:13

0 Answers0