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.
Script:
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 PyStreamCallback(StreamCallback):
def __init__(self):
pass
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
}
outputStream.write(bytearray(json.dumps(newObj).encode('utf-8')))
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