Dinosaurius
Dinosaurius

Reputation: 8628

How to put attributes of FlowFile into its JSON content?

I use ExecuteScript processor and Python language to write a script.

I want to pass two attributes (eventid and reason) of FlowFile into its JSON content as the parameter:value pair. The value of eventid is string, while the value of reason is integer. I tried to use flowFile.getAttribute('eventid'), but it fails.

What is the correct way?

def process(self, inputStream, outputStream):
        text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
        obj = json.loads(text)
        dt = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')

        newObj = {
            "EventId": str(parse(flowFile.getAttribute('eventid'))),
            "EventType": self.getEventType(dt,obj),
            "EventReason": flowFile.getAttribute('reason')
        }
        outputStream.write(bytearray(json.dumps(newObj, indent=4).encode('utf-8')))

flowFile = session.get()
if (flowFile != None):
    flowFile = session.write(flowFile, ModJSON())
session.transfer(flowFile, REL_SUCCESS)
session.commit()

Upvotes: 0

Views: 1194

Answers (1)

Bryan Bende
Bryan Bende

Reputation: 18640

You can use EvaluateJsonPath with Destination set to flow-file attributes and Return Type set to JSON. Then you can add properties for each JSON path to extract like:

eventid = $.eventid 

Upvotes: 2

Related Questions