diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ForEachMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ForEachMediator.java index 3483448b1d..72b3b3c7c7 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ForEachMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ForEachMediator.java @@ -129,6 +129,11 @@ public boolean mediate(MessageContext synCtx) { + " is not a valid JSON array", synCtx); } JsonArray iterableJsonArray = iterableChildElements.getAsJsonArray(); + // If the iterableJsonArray is empty, then no need to continue the mediation + if (iterableJsonArray.isEmpty()){ + log.info("No elements found for the JSONPath : " + expression); + return true; + } if (synLog.isTraceOrDebugEnabled()) { synLog.traceOrDebug("Splitting with JSONPath : " + expression + " resulted in " + iterableJsonArray.size() + " elements."); diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Target.java b/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Target.java index 1027629b42..1887073f9c 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Target.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Target.java @@ -527,9 +527,15 @@ private String removeJSONFromString(MessageContext synCtx, String inputString, S if (path.equals("$") || path.equals("$.")) { result = ""; } else { - DocumentContext doc = JsonPath.parse(result); - doc.delete(path); - result = doc.jsonString(); + Object list = JsonPath.compile(path).read(result); + if (!((JsonArray) list).isEmpty()) { + DocumentContext doc = JsonPath.parse(result); + doc.delete(path); + result = doc.jsonString(); + } else { + log.info("No matching elements were found for the given JSONPath: " + path + ". Therefore, " + + "no elements were removed."); + } } } }