How to get pretty array of JSON as groovy Script Output in NiFi
I am getting below input flow file:
Input:
[ {
"original" : {
"Id" : "100008",
"Date" : "2022-09-22"
},
"enrichment" : {
"Record" : "Record 11",
"Description" : "Invalid values for Id or Date"
}
}, {
"original" : {
"Id" : "100009",
"Date" : "2022-09-02"
},
"enrichment" : {
"Record" : "Record 2",
"Description" : "Invalid values for Id or Date"
}
} ]
Expected output:
[
{
"original" : {
"Id" : "100009",
"Date" : "2022-09-02"
},
"enrichment" : {
"Record" : "Record 2",
"Description" : "Invalid values for Id or Date"
}
}
]
and Output I am getting is the below using Groovy script:
[
"{\"original\":{\"Id\":\"100009\",\"Date\":\"2022-09-02\"},\"enrichment\":{\"Record\":\"Record 2\",\"Description\":\"Invalid values for Id or Date\"}}"
]
Groovy Script:
import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets
import groovy.json.*
import java.util.ArrayList
def flowFile = session.get()
if(!flowFile) return
try {
flowFile = session.write(flowFile,
{ inputStream, outputStream ->
def text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
def obj = new JsonSlurper().parseText(text)
def final1 = []
int j = obj.text.size()
for (int i=0;i<j;i++){
index = i+1
def rec = 'Record ' + index
if (rec in obj.enrichment.Record){
final1.add(JsonOutput.toJson(obj[i]))
}
}
response1 = JsonOutput.toJson(final1)
outputStream.write(response1.getBytes(StandardCharsets.UTF_8))
} as StreamCallback)
session.transfer(flowFile, REL_SUCCESS)
} catch(Exception e) {
log.error('Error during JSON operations', e)
session.transfer(flowFile, REL_FAILURE)
}
Can you please help to can I get the formatted array of json output of this groovy Script. I am new to groovy

