ExecuteScript python seems to execute different code
Hello, I have a problem with execute script processor. I have an incoming flowfile with json content like this:
{
"setName": "name",
"optionalDict": {},
"directories": [
{
"path":"path/to/file",
"fileName":"filename",
"id":1
}
],
"setId":12
} My script is the following:
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
from org.apache.nifi.processor.io import InputStreamCallback
flowFile = session.get()
if (flowFile != None):
stream_content = session.read(flowFile)
text_content = IOUtils.toString(stream_content, StandardCharsets.UTF_8)
IOUtils.closeQuietly(stream_content)
json_data = json.loads(text_content)
session.remove(flowFile)
d = {}
for x in range(len(json_data['directories'])):
d["flowfile{0}".format(x)] = None
for i, file in enumerate(d):
file = session.create()
file = session.putAttribute(file, "setId", str(json_data['setId']))
file = session.putAttribute(file, "setName", json_data["setName"])
file = session.putAttribute(file, "absolute.path", json_data['directories'][i]["path")
if json_data['optionalDict'] is not None:
if bool(json_data['optionalDict']):
file = session.putAttribute(file, "value1", str(json_data['set_entity_relation']['intValue']))
file = session.putAttribute(file, "value2", json_data['set_entity_relation']['stringValue'])
session.transfer(file, REL_SUCCESS)
session.commit()So what I aim to do is to commit one flowfile per entry in "directories" and write some values to the flowfiles attributes. This works fine if the "optionalDict" is not null and has the necessary entries. When it is an empty dict though, the script still tries to write the optional values and fails, since there are none. When I execute the same python code on my local scripts the if clauses prevent this.
Why does the executeScript processor ignore the two if clauses? I have tried different ways to stop it from executing the optional steps, but nothing works. Also it keeps telling me the error is on the line of the if clauses, when in reality it is two lines further down. This is the error message:
TypeError: putAttribute(): 3rd arg can't be coerced to String
Well, it shouldn't try to. What could I try to do?

