diff --git a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/EvaluateJsonPath.java b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/EvaluateJsonPath.java index e74783806a..13eb50b758 100644 --- a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/EvaluateJsonPath.java +++ b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/EvaluateJsonPath.java @@ -212,28 +212,31 @@ public class EvaluateJsonPath extends AbstractProcessor { String jsonPathAttrKey = attributeJsonPathEntry.getKey(); JsonPath jsonPathExp = attributeJsonPathEntry.getValue(); - final String evalResult = evaluatePathForContext(jsonPathExp, documentContext); + + final ObjectHolder resultHolder = new ObjectHolder<>(""); try { - switch (destination) { - case DESTINATION_ATTRIBUTE: - jsonPathResults.put(jsonPathAttrKey, evalResult); - break; - case DESTINATION_CONTENT: - flowFile = processSession.write(flowFile, new OutputStreamCallback() { - @Override - public void process(final OutputStream out) throws IOException { - try (OutputStream outputStream = new BufferedOutputStream(out)) { - outputStream.write(evalResult.getBytes(StandardCharsets.UTF_8)); - } - } - }); - break; - } + resultHolder.set(evaluatePathForContext(jsonPathExp, documentContext)); } catch (PathNotFoundException e) { logger.error("FlowFile {} could not find path {} for attribute key {}.", new Object[]{flowFile.getId(), jsonPathExp.getPath(), jsonPathAttrKey}, e); jsonPathResults.put(jsonPathAttrKey, ""); } + + switch (destination) { + case DESTINATION_ATTRIBUTE: + jsonPathResults.put(jsonPathAttrKey, resultHolder.get()); + break; + case DESTINATION_CONTENT: + flowFile = processSession.write(flowFile, new OutputStreamCallback() { + @Override + public void process(final OutputStream out) throws IOException { + try (OutputStream outputStream = new BufferedOutputStream(out)) { + outputStream.write(resultHolder.get().getBytes(StandardCharsets.UTF_8)); + } + } + }); + break; + } } flowFile = processSession.putAllAttributes(flowFile, jsonPathResults); processSession.transfer(flowFile, REL_MATCH); diff --git a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestEvaluateJsonPath.java b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestEvaluateJsonPath.java index 60e19d91c4..6a1fbad460 100644 --- a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestEvaluateJsonPath.java +++ b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestEvaluateJsonPath.java @@ -117,6 +117,28 @@ public class TestEvaluateJsonPath { Assert.assertEquals("Transferred flow file did not have the correct result for name attribute", "{\"first\":\"Shaffer\",\"last\":\"Pearson\"}", out.getAttribute(jsonPathNameAttrKey)); } + @Test + public void testExtractPath_destinationAttributes_twoPaths_notFound() throws Exception { + final TestRunner testRunner = TestRunners.newTestRunner(new EvaluateJsonPath()); + testRunner.setProperty(EvaluateJsonPath.DESTINATION, EvaluateJsonPath.DESTINATION_ATTRIBUTE); + + String jsonPathIdAttrKey = "evaluatejson.id"; + String jsonPathNameAttrKey = "evaluatejson.name"; + + testRunner.setProperty(jsonPathIdAttrKey, "$[0]._id.nonexistent"); + testRunner.setProperty(jsonPathNameAttrKey, "$[0].name.nonexistent"); + + testRunner.enqueue(JSON_SNIPPET); + testRunner.run(); + + Relationship expectedRel = EvaluateJsonPath.REL_MATCH; + + testRunner.assertAllFlowFilesTransferred(expectedRel, 1); + final MockFlowFile out = testRunner.getFlowFilesForRelationship(expectedRel).get(0); + Assert.assertEquals("Transferred flow file did not have the correct result for id attribute", "", out.getAttribute(jsonPathIdAttrKey)); + Assert.assertEquals("Transferred flow file did not have the correct result for name attribute", "", out.getAttribute(jsonPathNameAttrKey)); + } + @Test public void testExtractPath_destinationContent() throws Exception { String jsonPathAttrKey = "JsonPath";