Duplicate object keys handling change with Jackson 2.13 FAIL_ON_READING_DUP_TREE_KEY
0 reputation · 06 Oct 2021, 16:18 UTC
0 reputation · 06 Oct 2021, 16:18 UTC
The goal is to define a consistent duplicate-key policy for JSON objects consumed across services that use different parsers and library versions. RFC 8259 does not prescribe how duplicate object keys must be handled, leaving the behavior implementation-specific.
The compatibility boundary centers on Jackson 2.13, which added the mapper feature FAIL_ON_READING_DUP_TREE_KEY to reject duplicates, whereas Jackson 2.12 and earlier silently used the last value. Gson, Python’s built-in json module, and Node.js JSON.parse also adopt last-wins semantics with no built-in option to detect duplicates.
With heterogeneous runtimes in use, the decision is whether to enforce strict rejection, allow silent last-wins, or normalize inputs before parsing.
What configuration transition is appropriate when moving from Jackson 2.12 to 2.13+ without breaking existing payloads? How can duplicate keys be detected in environments that provide no built-in detection? Should duplicate keys be treated as a schema violation consistently across all parsers?
35275 reputation · 06 Oct 2021, 22:46 UTC
To move from Jackson 2.12 to 2.13+ without breaking existing payloads, you must explicitly maintain the "last-wins" behavior. By default, FAIL_ON_READING_DUP_TREE_KEY is disabled, but if your organization is moving toward strict validation, do not enable this feature globally until all upstream producers are audited.
JsonParser.Feature.STRICT_DUPLICATE_DETECTION (or the mapper-level FAIL_ON_READING_DUP_TREE_KEY) remains false during the initial upgrade.ObjectMapper instances used for new, strictly-governed APIs.Since Gson, Node.js JSON.parse(), and Python's json module default to last-wins without native detection toggles, you must implement a custom interception layer. The most reliable method is to use a streaming parser or a custom object hook.
object_pairs_hook argument in json.loads(). This passes a list of tuples instead of a dictionary, allowing you to check for duplicate keys manually.json-source-map or a custom parser that implements a reviver function, though native JSON.parse cannot detect duplicates once the object is constructed.Duplicate keys should be treated as a schema violation if the data is used for security-critical operations (e.g., authentication claims or financial totals) to prevent "JSON Injection" or "Parameter Pollution" attacks where different parsers see different values.
Assumption: This guidance assumes the use of standard ObjectMapper configurations. If using custom Deserializers, the FAIL_ON_READING_DUP_TREE_KEY feature may not automatically trigger unless the deserializer relies on the underlying tree model.
// Java snippet to verify strict detection
ObjectMapper mapper = JsonMapper.builder()
.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION)
.build();
// This will throw JsonProcessingException if duplicates exist
mapper.readTree("{\"key\": 1, \"key\": 2}");
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.