<?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 Update an attribute within another attribute in Apache Nifi in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Update-an-attribute-within-another-attribute-in-Apache-Nifi/m-p/235675#M197488</link>
    <description>&lt;P&gt;I have a workflow which loads JSON templates from SQL which contain attribute placeholders:&lt;/P&gt;&lt;PRE&gt;{
&amp;nbsp; &amp;nbsp; "agent": "${hostname(true)}",
&amp;nbsp; &amp;nbsp; "startDate": "${startDate}",
&amp;nbsp; &amp;nbsp; "endDate": "${endDate}",
&amp;nbsp; &amp;nbsp;&amp;nbsp;"data": "${data}"
}&lt;/PRE&gt;&lt;P&gt;These templates contain placeholder attributes which I'd like to replace with the value of real attributes in the Flowfile e.g. ${endDate}&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="109884-1563373661265.png" style="width: 131px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/14508i5A060DB6FDA541CF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="109884-1563373661265.png" alt="109884-1563373661265.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The SQL results are converted to JSON using ConvertAvroToJSON and then converted to an attribute named: analysisMethodArgs using EvaluateJsonPath.&lt;/P&gt;&lt;P&gt;I've tried ReplaceText to replace the Flowfile content with the analysisMethodArgs attribute, but it still results in the placeholders in the text and not the actual attribute values.&lt;/P&gt;&lt;P&gt;Is this possible?&lt;/P&gt;</description>
    <pubDate>Sat, 17 Aug 2019 23:46:36 GMT</pubDate>
    <dc:creator>tomp</dc:creator>
    <dc:date>2019-08-17T23:46:36Z</dc:date>
    <item>
      <title>Update an attribute within another attribute in Apache Nifi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Update-an-attribute-within-another-attribute-in-Apache-Nifi/m-p/235675#M197488</link>
      <description>&lt;P&gt;I have a workflow which loads JSON templates from SQL which contain attribute placeholders:&lt;/P&gt;&lt;PRE&gt;{
&amp;nbsp; &amp;nbsp; "agent": "${hostname(true)}",
&amp;nbsp; &amp;nbsp; "startDate": "${startDate}",
&amp;nbsp; &amp;nbsp; "endDate": "${endDate}",
&amp;nbsp; &amp;nbsp;&amp;nbsp;"data": "${data}"
}&lt;/PRE&gt;&lt;P&gt;These templates contain placeholder attributes which I'd like to replace with the value of real attributes in the Flowfile e.g. ${endDate}&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="109884-1563373661265.png" style="width: 131px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/14508i5A060DB6FDA541CF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="109884-1563373661265.png" alt="109884-1563373661265.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The SQL results are converted to JSON using ConvertAvroToJSON and then converted to an attribute named: analysisMethodArgs using EvaluateJsonPath.&lt;/P&gt;&lt;P&gt;I've tried ReplaceText to replace the Flowfile content with the analysisMethodArgs attribute, but it still results in the placeholders in the text and not the actual attribute values.&lt;/P&gt;&lt;P&gt;Is this possible?&lt;/P&gt;</description>
      <pubDate>Sat, 17 Aug 2019 23:46:36 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Update-an-attribute-within-another-attribute-in-Apache-Nifi/m-p/235675#M197488</guid>
      <dc:creator>tomp</dc:creator>
      <dc:date>2019-08-17T23:46:36Z</dc:date>
    </item>
    <item>
      <title>Re: Update an attribute within another attribute in Apache Nifi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Update-an-attribute-within-another-attribute-in-Apache-Nifi/m-p/269827#M207115</link>
      <description>&lt;P&gt;A ExecuteScript for those who also need to do this and get valid JSON back:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import re
import sys
import copy
import json
import traceback
from java.nio.charset import StandardCharsets
from org.apache.commons.io import IOUtils
from org.apache.nifi.processor.io import StreamCallback
from org.python.core import PyFile
from org.python.core.util import FileUtil, StringUtil

def remove_controls(text):

    out = re.sub('\n', '', text)
    out = re.sub('\r', '', out)
    return out

class TransformCallback(StreamCallback):
    def __init__(self, flowFile):
        self.flowFile = flowFile

    def process(self, inputStream, outputStream):
        try:
            attrs = self.flowFile.getAttributes()
            pf = FileUtil().wrap(inputStream)
            output = []
            for line in pf.readlines():

                for atr in dict(attrs).keys():

                    try:
                        sub = attrs[atr].encode('ascii', 'ignore')
                        sub = remove_controls(sub)
                    except:
                        sub = attrs[atr]
                    
                    line = re.sub('\${' + atr + '}', sub, line)

                output.append(line.rstrip("\n"))
            try:
                json.dumps(output)
            except:
                raise Exception("COULD NOT POST INVALID JSON")

            outputStream.write('\n'.join(output))
        except:
            traceback.print_exc(file=sys.stdout)
            raise

flowFiles = session.get(10)
for flowFile in flowFiles:
    if flowFile is None:
        continue
    flowFile = session.write(flowFile, TransformCallback(flowFile))
    session.transfer(flowFile, REL_SUCCESS)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Sep 2019 12:55:32 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Update-an-attribute-within-another-attribute-in-Apache-Nifi/m-p/269827#M207115</guid>
      <dc:creator>tomp</dc:creator>
      <dc:date>2019-09-05T12:55:32Z</dc:date>
    </item>
  </channel>
</rss>

