<?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 PyStreamCallBack object has no attribute  length...  ERROR in NiFi in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/PyStreamCallBack-object-has-no-attribute-length-ERROR-in/m-p/346283#M234825</link>
    <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;I am trying to&amp;nbsp; read&amp;nbsp; JSON body from a flowfile and write the length of a JSON array of elements to new flowfile attribute. But somehow I am getting aforementioned error all the time.&lt;BR /&gt;Here is my script.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
import json

#Define a subclass of StreamCallback for use in session.write()
class PyStreamCallback(StreamCallback):
  def __init__(self):
        pass
  def process(self, inputStream, outputStream):
    jsn = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
    array = json.loads(jsn) # type: dict
    i = 0
    while i &amp;lt;= 1:  # ищем вложенность второго уровня, чтобы посчитать кол-во элементов (записей в таблице)
      root_key = list(array.keys())[0]
      array = array[root_key]
      i += 1
    self.length = str(len(array))

  def get_length_of_array(self):
    return self.length

# end class
flowfile = session.get()
if(flowfile != None):
    flowfile = session.write(flowfile, PyStreamCallback())
    flowfile = session.putAttribute(flowfile, "length", PyStreamCallback().get_length_of_array())
    session.transfer(flowFile, REL_SUCCESS)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone please explain me what am I doing wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you beforehand!&lt;/P&gt;</description>
    <pubDate>Fri, 24 Jun 2022 09:25:16 GMT</pubDate>
    <dc:creator>Brenigan</dc:creator>
    <dc:date>2022-06-24T09:25:16Z</dc:date>
    <item>
      <title>PyStreamCallBack object has no attribute  length...  ERROR in NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/PyStreamCallBack-object-has-no-attribute-length-ERROR-in/m-p/346283#M234825</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;I am trying to&amp;nbsp; read&amp;nbsp; JSON body from a flowfile and write the length of a JSON array of elements to new flowfile attribute. But somehow I am getting aforementioned error all the time.&lt;BR /&gt;Here is my script.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
import json

#Define a subclass of StreamCallback for use in session.write()
class PyStreamCallback(StreamCallback):
  def __init__(self):
        pass
  def process(self, inputStream, outputStream):
    jsn = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
    array = json.loads(jsn) # type: dict
    i = 0
    while i &amp;lt;= 1:  # ищем вложенность второго уровня, чтобы посчитать кол-во элементов (записей в таблице)
      root_key = list(array.keys())[0]
      array = array[root_key]
      i += 1
    self.length = str(len(array))

  def get_length_of_array(self):
    return self.length

# end class
flowfile = session.get()
if(flowfile != None):
    flowfile = session.write(flowfile, PyStreamCallback())
    flowfile = session.putAttribute(flowfile, "length", PyStreamCallback().get_length_of_array())
    session.transfer(flowFile, REL_SUCCESS)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone please explain me what am I doing wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you beforehand!&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jun 2022 09:25:16 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/PyStreamCallBack-object-has-no-attribute-length-ERROR-in/m-p/346283#M234825</guid>
      <dc:creator>Brenigan</dc:creator>
      <dc:date>2022-06-24T09:25:16Z</dc:date>
    </item>
    <item>
      <title>Re: PyStreamCallBack object has no attribute  length...  ERROR in NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/PyStreamCallBack-object-has-no-attribute-length-ERROR-in/m-p/346325#M234839</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/97722"&gt;@Brenigan&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The issue that you are seeing is because you are instantiating PyStreamCallback twice. You should do it once and reference that object in the subsequent calls to the session functions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code below works as you'd expect:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
import json

#Define a subclass of StreamCallback for use in session.write()
class PyStreamCallback(StreamCallback):
  def __init__(self):
      self.length = 0

  def process(self, inputStream, outputStream):
      jsn = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
      array = json.loads(jsn) # type: dict
      i = 0
      while i &amp;lt;= 1:
        root_key = list(array.keys())[0]
        array = array[root_key]
        i += 1
      self.length = str(len(array))

  def get_length_of_array(self):
      return self.length

# end class
flowfile = session.get()
if(flowfile != None):
    reader = PyStreamCallback()
    flowfile = session.write(flowfile, reader)
    flowfile = session.putAttribute(flowfile, "length", reader.get_length_of_array())
    session.transfer(flowfile, REL_SUCCESS)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is a simpler way to do what you're trying to do, though. For example, say you have the following JSON object in the incoming flowfile:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{
  "root": {
    "items": [1, 2]
  }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you want to set the flowfile "length" attribute with the&amp;nbsp;length of the "items" array, you can simply use the EvaluateJsonPath processor with the following configuration:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="araujo_0-1656134097658.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/34678i5BB39927944DAE16/image-size/medium?v=v2&amp;amp;px=400" role="button" title="araujo_0-1656134097658.png" alt="araujo_0-1656134097658.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;André&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 25 Jun 2022 05:15:28 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/PyStreamCallBack-object-has-no-attribute-length-ERROR-in/m-p/346325#M234839</guid>
      <dc:creator>araujo</dc:creator>
      <dc:date>2022-06-25T05:15:28Z</dc:date>
    </item>
  </channel>
</rss>

