{"record":{"id":"05422174f28bef68","repo":"Activiti/Activiti","slug":"duplicate-key-key","errorCode":null,"errorMessage":"Duplicate key \\\"\" + key + \"\\\"","messagePattern":"Duplicate key \\\\\"\" \\+ key \\+ \"\\\\\"","errorType":"exception","errorClass":"JSONException","httpStatus":null,"severity":"error","filePath":"activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/util/json/JSONObject.java","lineNumber":1004,"sourceCode":"        } else {\n            remove(key);\n        }\n        return this;\n    }\n\n    /**\n     * Put a key/value pair in the JSONObject, but only if the key and the value are both non-null, and only if there is not already a member with that name.\n     *\n     * @param key\n     * @param value\n     * @return his.\n     * @throws JSONException\n     *           if the key is a duplicate\n     */\n    public JSONObject putOnce(String key, Object value) throws JSONException {\n        if (key != null && value != null) {\n            if (opt(key) != null) {\n                throw new JSONException(\"Duplicate key \\\"\" + key + \"\\\"\");\n            }\n            put(key, value);\n        }\n        return this;\n    }\n\n    /**\n     * Put a key/value pair in the JSONObject, but only if the key and the value are both non-null.\n     *\n     * @param key\n     *          A key string.\n     * @param value\n     *          An object which is the value. It should be of one of these types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONObject.NULL object.\n     * @return this.\n     * @throws JSONException\n     *           If the value is a non-finite number.\n     */\n    public JSONObject putOpt(String key, Object value) throws JSONException {","sourceCodeStart":986,"sourceCodeEnd":1022,"githubUrl":"https://github.com/Activiti/Activiti/blob/56435b1a97deeafdc09dd40074b056c89fba5a8a/activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/util/json/JSONObject.java#L986-L1022","documentation":"putOnce(key, value) adds a pair only if the key is not already present; when opt(key) returns an existing value it throws JSONException(\"Duplicate key \\\"<key>\\\"\"). It is used by constructors building a JSONObject from another source that should have unique keys.","triggerScenarios":"Calling putOnce with a key that already exists in the JSONObject, or constructing a JSONObject from a Map/bean where two entries collapse to the same key name (e.g. bean property name collision).","commonSituations":"Merging two JSON documents with overlapping keys; building from a map where case differences were expected to be distinct but were normalized; reflecting over beans producing duplicate property names.","solutions":["Check opt(key) == null before calling putOnce, or use plain put(key, value) to intentionally overwrite","Deduplicate the source map before constructing the JSONObject","Decide the merge policy: skip, overwrite, or fail — and use the matching API (putOnce, put, or explicit check)","Fix upstream data so keys are genuinely unique"],"exampleFix":"// before\njson.putOnce(\"id\", idValue); // throws if \"id\" already set\n// after\nif (json.opt(\"id\") == null) {\n    json.putOnce(\"id\", idValue);\n} else {\n    json.put(\"id\", idValue); // explicit overwrite policy\n}","handlingStrategy":"validation","validationCode":"if (json.opt(key) != null) { throw new IllegalStateException(\"duplicate key: \" + key); }","typeGuard":"boolean isKeyFree(JSONObject src, String key) { return src.opt(key) == null; }","tryCatchPattern":"try { json.putOnce(key, value); } catch (JSONException e) { log.warn(\"duplicate key \" + key + \" — skipping\", e); }","preventionTips":["Use put() when overwrite-on-duplicate is the intended behavior","Deduplicate source maps before constructing JSONObject","Define an explicit merge policy (skip/overwrite/fail) for multi-source documents"],"tags":["json","duplicate-key","activiti"],"backgroundTag":"schema-validation-failed","analyzedSha":"56435b1a97deeafdc09dd40074b056c89fba5a8a","analyzedAt":"2026-09-09T21:00:06.703Z","contentChangedAt":"2026-09-09T21:00:06.703Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}