Access Json element and write to a text file using python ExecuteScript processor
<p><a href="https://i.stack.imgur.com/u6OoC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/u6OoC.png" alt="flow"></a></p>

<p>I am new to python and nifi.</p>

<p>My flow is GetFile-->ExecuteScript</p>

<p>In the script, for each json,i want to accesss a particular element and write it to a text file line by line. </p>

<p>I tried the below:</p>

<pre><code>import json
import java.io
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback

class ModJSON(StreamCallback):
  def __init__(self):
    pass
  def process(self, inputStream, outputStream):
  text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
  json_content = json.loads(text)
  try:
     body = json_content['id']['body']
     body_encoded = body.encode('utf-8')
  except (KeyError,TypeError,ValueError):
     body_encoded = ''

  text_file = open ('/tmp/test/testFile.txt', 'w')   
  text_file.write("%s"%body_encoded)
  text_file.close()
  outputStream.write(bytearray(json.dumps(body, indent=4).encode('utf-8')))

flowFile = session.get()
if (flowFile != None):
    flowFile = session.write(flowFile, ModJSON())
    flowFile = session.putAttribute(flowFile, "filename", flowFile.getAttribute('filename').split('.')[0]+'_translated.json')
session.transfer(flowFile, REL_SUCCESS)
</code></pre>

<p>but in the testFile.txt, the accessed body is not being written.</p>

<p>what do i miss here?</p>