Support Questions
Find answers, ask questions, and share your expertise
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

Python script for ExecuteScript processor

Python script for ExecuteScript processor

Hi All,

I am new to NiFi. I am hoping you guys will be able to help me out. I have the below mentioned python script to extract fields from a JSON payload. But the problem i am facing is suppose lets say there's no key value pair present in the payload, so how do i set it to null if the value is not present in the JSON. I tried with using ternary operator but the script failed. The below code is just a sample of what I am trying to achieve. In actuality I need to extract 100 fields from payload so using EvaluateJson processor is not feasible.

import json
from import IOUtils
from java.nio.charset import StandardCharsets
from import StreamCallback
class PyStreamCallback(StreamCallback):
def __init__(self):
def process(self, inputStream, outputStream):
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
obj = json.loads(text)
newObj = {
"Name": obj['name'],
"Age": obj['age'],
"Gender" : obj['gender'],
"test": obj['test'] if obj['test'] else "NULL" //trying to extract test value from Json payload if not present then set it to NULL
flowFile = session.get()
if (flowFile != None):
flowFile = session.write(flowFile,PyStreamCallback())
flowFile = session.putAttribute(flowFile, "filename", flowFile.getAttribute('filename').split('.')[0]+'_translated.json') session.transfer(flowFile, REL_SUCCESS)

-Thanks In Advance


Re: Python script for ExecuteScript processor

Super Guru

Python/Jython doesn't have a "null" object, try None instead

Don't have an account?
Coming from Hortonworks? Activate your account here