{"record":{"id":"33b3801823fcd9da","repo":"google/gson","slug":"duplicate-key-key","errorCode":null,"errorMessage":"duplicate key: ${key}","messagePattern":"duplicate key: (.+?)","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/MapTypeAdapterFactory.java","lineNumber":192,"sourceCode":"\n    @Override\n    public Map<K, V> read(JsonReader in) throws IOException {\n      JsonToken peek = in.peek();\n      if (peek == JsonToken.NULL) {\n        in.nextNull();\n        return null;\n      }\n\n      Map<K, V> map = constructor.construct();\n\n      if (peek == JsonToken.BEGIN_ARRAY) {\n        in.beginArray();\n        while (in.hasNext()) {\n          in.beginArray(); // entry array\n          K key = keyTypeAdapter.read(in);\n          V value = valueTypeAdapter.read(in);\n          if (map.containsKey(key)) {\n            throw new JsonSyntaxException(\"duplicate key: \" + key);\n          }\n          map.put(key, value);\n          in.endArray();\n        }\n        in.endArray();\n      } else {\n        in.beginObject();\n        while (in.hasNext()) {\n          JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in);\n          K key = keyTypeAdapter.read(in);\n          V value = valueTypeAdapter.read(in);\n          if (map.containsKey(key)) {\n            throw new JsonSyntaxException(\"duplicate key: \" + key);\n          }\n          map.put(key, value);\n        }\n        in.endObject();\n      }","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/MapTypeAdapterFactory.java#L174-L210","documentation":"When Gson deserializes a Map from the array-of-entries JSON form (used with complex map key serialization), MapTypeAdapterFactory checks each key and throws JsonSyntaxException(\"duplicate key: ...\") if a key already exists in the map. This enforces map integrity at deserialization time rather than silently overwriting an entry.","triggerScenarios":"Deserializing to Map<K,V> from JSON shaped like [[\"k\",v1],[\"k\",v2]] (array form) where the same key k appears more than once; data generated without key uniqueness guarantees.","commonSituations":"Malformed or duplicated source data; aggregation pipelines that emit repeated keys; switching a producer from object form to array form without deduplication.","solutions":["Remove duplicate keys at the data source before serialization","Pre-process the JSON (or the JsonElement tree) to deduplicate entries before fromJson","If duplicates are semantically acceptable, deserialize to a List of pairs and dedupe/merge in Java"],"exampleFix":"// before - JSON has duplicate keys (array form)\n[[\"k\",1],[\"k\",2]]\n\n// after - deduplicated source\n[[\"k\",1]]\n\n// or merge duplicates in Java before building the map","handlingStrategy":"validation","validationCode":"// Deduplicate keys before deserialization (array-of-entries form)\nJsonArray arr = JsonParser.parseString(json).getAsJsonArray();\njava.util.Set<String> seen = new java.util.HashSet<>();\nJsonArray deduped = new JsonArray();\nfor (JsonElement e : arr) {\n    String k = e.getAsJsonArray().get(0).getAsString();\n    if (seen.add(k)) deduped.add(e);\n}\nMap<String,V> map = gson.fromJson(deduped, mapType);","typeGuard":null,"tryCatchPattern":"try {\n  Map<K,V> m = gson.fromJson(json, mapType);\n} catch (com.google.gson.JsonSyntaxException e) {\n  // 'duplicate key': pre-dedupe the source and retry, or deserialize to a List and merge\n}","preventionTips":["Validate source data for key uniqueness at ingestion","Use a JsonObject/JsonArray pre-parse step to deduplicate before fromJson","If duplicates are valid, deserialize to a List of pairs and merge in Java"],"tags":["json","deserialization","map","duplicate-keys"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}