Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Support Questions

Find answers, ask questions, and share your expertise
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!

Getting Empty Array Using Groovy Script in Nifi

avatar
Contributor

Hi all,

 

I have an requirement where I need to parse the data into the required format

Input:

 

{
"Message" : "\nRecord 1:\nRequired data is missing. \n\nRecord 2:\nprocessing failed\n"
}

 

Here the content and delimiters are not fixed. The fixed part is only /nRecord keyword on which I am writing the Script. But I am not getting desired Output using Groovy.

 

desired Output:

 

[{
"Record 1": "nRequired data is missing"
}, {
"Record 2": "processing failed"
}]

 

I have written Groovy Script for the same but I am getting empty array.

 

import org.apache.commons.io.IOUtils
import groovy.json.*
import java.util.ArrayList
import java.nio.charset.*
import java.nio.charset.StandardCharsets
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder


def flowFile = session.get()
if(!flowFile) return
try {
flowFile = session.write(flowFile,
{ inputStream, outputStream ->
def text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
splitted = text.split('\nRecord')
int j = splitted.size()
final1 = []
for (int i=0;i<j-1;i++)
{
k = "Record " + splitted[i+1]
valid = k.replaceAll("\\n|\"|\\n|}","")
final1.add("{\"" + valid.replaceFirst(":",'":"')+ "\"}" )
}
def json = JsonOutput.toJson(final1)
outputStream.write(JsonOutput.prettyPrint(json).getBytes(StandardCharsets.UTF_8))
} as StreamCallback)

session.transfer(flowFile, REL_SUCCESS)
} catch(Exception e) {
log.error('Error during JSON operations', e)
flowFile = session.putAttribute(flowFile, "error", e.getMessage())
session.transfer(flowFile, REL_FAILURE)
}

Can you please help me with the same.

Thank you. 

1 REPLY 1

avatar
Super Collaborator

This is Groovy code that achieves what you've mentioned and should be able to adapt to your scripted processor.

import groovy.json.JsonOutput
import groovy.json.JsonSlurper

JsonSlurper jsonSlurper = new JsonSlurper()
JsonOutput jsonOutput = new JsonOutput()

Map<String, String> data = jsonSlurper.parseText('''{
  "Message": "\nRecord 1:\nRequired data is missing. \n\nRecord 2:\nprocessing failed\n"
}''')

println(data)
println("=" * 80)

List<String> records = data.Message.split("\\nRecord\\s\\d+:\\n").findAll { it.trim() != "" }
println(records)
println("=" * 80)

Map<String, String> messages = [:]
records.eachWithIndex { message, index ->
    messages["Record ${index + 1}"] = message.trim()
}

println(jsonOutput.toJson(messages))

This is the output:

[Message:
Record 1:
Required data is missing. 

Record 2:
processing failed
]
================================================================================
[Required data is missing. 
, processing failed
]
================================================================================
{"Record 1":"Required data is missing.","Record 2":"processing failed"}