<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: ExecuteScript - flowFile with new attributes not sent to next processor in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/ExecuteScript-flowFile-with-new-attributes-not-sent-to-next/m-p/357187#M237499</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in your script body you dont manipulate the content.&lt;/P&gt;&lt;P&gt;You add an Attribute to your flowfile (which works &lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;)&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe you can have a look at&amp;nbsp;&lt;A href="https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-2/ta-p/249018/page/2" target="_blank"&gt;https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-2/ta-p/249018/page/2&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But in case you don´t need to do it via ExecuteScript Processor, you could use JoltTransformJSON Processor.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;On conficuration you can set for the property&amp;nbsp;&lt;STRONG&gt;Jolt Specification &lt;/STRONG&gt;following value:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[
  {
    "operation": "default",
    "spec": {
      "gender": "male"
    }
  }
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;After that your JSON should be extended with your key-value-pair.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Greetings&lt;/P&gt;</description>
    <pubDate>Tue, 08 Nov 2022 10:00:41 GMT</pubDate>
    <dc:creator>Faerballert</dc:creator>
    <dc:date>2022-11-08T10:00:41Z</dc:date>
    <item>
      <title>ExecuteScript - flowFile with new attributes not sent to next processor</title>
      <link>https://community.cloudera.com/t5/Support-Questions/ExecuteScript-flowFile-with-new-attributes-not-sent-to-next/m-p/357185#M237497</link>
      <description>&lt;P&gt;I have a &lt;STRONG&gt;GenerateFlowFile&lt;/STRONG&gt; processor that sends a flowFile to the &lt;STRONG&gt;ExecuteScript&lt;/STRONG&gt; processor. In the &lt;STRONG&gt;ExecuteScript&lt;/STRONG&gt; processor, I added a new attribute and value to the incoming flowFIle. But this updated flowFIle is not output to the next processor&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Incoming FlowFile content from GenerateFlowFile&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{'name':'myName'}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;ExecuteScript script body&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var flowFile = session.get();
if (flowFile != null) {
	try {
		flowFile = session.putAttribute(flowFile, 'gender', 'male');
		session.transfer(flowFile, REL_SUCCESS);
	} catch (e) {
		session.transfer(flowFile, REL_FAILURE);
	}
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Output to next processor&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{'name':'myName'}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Am I missing something??&lt;/P&gt;</description>
      <pubDate>Tue, 08 Nov 2022 09:37:28 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/ExecuteScript-flowFile-with-new-attributes-not-sent-to-next/m-p/357185#M237497</guid>
      <dc:creator>chrizonline</dc:creator>
      <dc:date>2022-11-08T09:37:28Z</dc:date>
    </item>
    <item>
      <title>Re: ExecuteScript - flowFile with new attributes not sent to next processor</title>
      <link>https://community.cloudera.com/t5/Support-Questions/ExecuteScript-flowFile-with-new-attributes-not-sent-to-next/m-p/357187#M237499</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in your script body you dont manipulate the content.&lt;/P&gt;&lt;P&gt;You add an Attribute to your flowfile (which works &lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;)&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe you can have a look at&amp;nbsp;&lt;A href="https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-2/ta-p/249018/page/2" target="_blank"&gt;https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-2/ta-p/249018/page/2&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But in case you don´t need to do it via ExecuteScript Processor, you could use JoltTransformJSON Processor.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;On conficuration you can set for the property&amp;nbsp;&lt;STRONG&gt;Jolt Specification &lt;/STRONG&gt;following value:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[
  {
    "operation": "default",
    "spec": {
      "gender": "male"
    }
  }
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;After that your JSON should be extended with your key-value-pair.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Greetings&lt;/P&gt;</description>
      <pubDate>Tue, 08 Nov 2022 10:00:41 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/ExecuteScript-flowFile-with-new-attributes-not-sent-to-next/m-p/357187#M237499</guid>
      <dc:creator>Faerballert</dc:creator>
      <dc:date>2022-11-08T10:00:41Z</dc:date>
    </item>
    <item>
      <title>Re: ExecuteScript - flowFile with new attributes not sent to next processor</title>
      <link>https://community.cloudera.com/t5/Support-Questions/ExecuteScript-flowFile-with-new-attributes-not-sent-to-next/m-p/357234#M237515</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for pointing that out. It has certaintly speed up my development.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a short snippet to my solution:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;var StreamCallback =  Java.type("org.apache.nifi.processor.io.StreamCallback");
var IOUtils = Java.type("org.apache.commons.io.IOUtils");
var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");

var flowFile = session.get();
if(flowFile != null) {
	try {
	  // Create a new StreamCallback, passing in a function to define the interface method
	  flowFile = session.write(flowFile,
	    new StreamCallback(function(inputStream, outputStream) {
	        var text = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
					var obj = JSON.parse(text);
					obj["gender"] = "Male";
					outputStream.write(JSON.stringify(obj).getBytes(StandardCharsets.UTF_8));
	    }));
		session.transfer(flowFile, REL_SUCCESS);
	} catch (e) {
			log.error(e.message)
			session.transfer(flowFile, REL_FAILURE);
	}
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Something to point out for those using javascript. If your using &lt;STRONG&gt;GenerateFlowFile processor&lt;/STRONG&gt; to generate sample JSON content, the JSON content in Custom Text must use double quotes ( " ), not single quotes ( ' ). I spent an hour debugging why my JSON.parse(text) didn't work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Custom Text&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{'name':'myName'}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It should be&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{"name":"myName"}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2022 03:38:43 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/ExecuteScript-flowFile-with-new-attributes-not-sent-to-next/m-p/357234#M237515</guid>
      <dc:creator>chrizonline</dc:creator>
      <dc:date>2022-11-09T03:38:43Z</dc:date>
    </item>
  </channel>
</rss>

