Activiti/Activiti · error · JSONException
Duplicate key \"" + key + "\"
Error message
Duplicate key \"" + key + "\"
What it means
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.
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
Example fix
// before
json.putOnce("id", idValue); // throws if "id" already set
// after
if (json.opt("id") == null) {
json.putOnce("id", idValue);
} else {
json.put("id", idValue); // explicit overwrite policy
} Defensive patterns
Strategy: validation
Validate before calling
if (json.opt(key) != null) { throw new IllegalStateException("duplicate key: " + key); } Type guard
boolean isKeyFree(JSONObject src, String key) { return src.opt(key) == null; } Try / catch
try { json.putOnce(key, value); } catch (JSONException e) { log.warn("duplicate key " + key + " — skipping", e); } Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- Bad value from toJSONString: " + o
- e
- Error deserializing json info for process definition
- JSON does not allow non-finite numbers.
- JSONObject[" + quote(key) + "] is not a JSONObject.
AI-assisted analysis of Activiti/Activiti@56435b1a97 (2026-09-09).
Data as JSON: /api/errors/05422174f28bef68.
Report an issue: GitHub.
Appendix: source
Thrown at activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/util/json/JSONObject.java:1004
} else {
remove(key);
}
return this;
}
/**
* 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.
*
* @param key
* @param value
* @return his.
* @throws JSONException
* if the key is a duplicate
*/
public JSONObject putOnce(String key, Object value) throws JSONException {
if (key != null && value != null) {
if (opt(key) != null) {
throw new JSONException("Duplicate key \"" + key + "\"");
}
put(key, value);
}
return this;
}
/**
* Put a key/value pair in the JSONObject, but only if the key and the value are both non-null.
*
* @param key
* A key string.
* @param value
* 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.
* @return this.
* @throws JSONException
* If the value is a non-finite number.
*/
public JSONObject putOpt(String key, Object value) throws JSONException {View on GitHub (pinned to 56435b1a97)