<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Getting Empty Array Using Groovy Script in  Nifi in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Getting-Empty-Array-Using-Groovy-Script-in-Nifi/m-p/361014#M238494</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have an requirement where I need to parse the data into the required format&lt;/P&gt;&lt;P&gt;Input:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"Message" : "\nRecord 1:\nRequired data is missing. \n\nRecord 2:\nprocessing failed\n"&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;desired Output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;[{&lt;BR /&gt;"Record 1": "nRequired data is missing"&lt;BR /&gt;}, {&lt;BR /&gt;"Record 2": "processing failed"&lt;BR /&gt;}]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have written Groovy Script for the same but I am getting empty array.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import org.apache.commons.io.IOUtils&lt;BR /&gt;import groovy.json.*&lt;BR /&gt;import java.util.ArrayList&lt;BR /&gt;import java.nio.charset.*&lt;BR /&gt;import java.nio.charset.StandardCharsets&lt;BR /&gt;import groovy.json.JsonSlurper&lt;BR /&gt;import groovy.json.JsonBuilder&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;def flowFile = session.get()&lt;BR /&gt;if(!flowFile) return&lt;BR /&gt;try {&lt;BR /&gt;flowFile = session.write(flowFile,&lt;BR /&gt;{ inputStream, outputStream -&amp;gt;&lt;BR /&gt;def text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)&lt;BR /&gt;splitted = text.split('\nRecord')&lt;BR /&gt;int j = splitted.size()&lt;BR /&gt;final1 = []&lt;BR /&gt;for (int i=0;i&amp;lt;j-1;i++)&lt;BR /&gt;{&lt;BR /&gt;k = "Record " + splitted[i+1]&lt;BR /&gt;valid = k.replaceAll("\\n|\"|\\n|}","")&lt;BR /&gt;final1.add("{\"" + valid.replaceFirst(":",'":"')+ "\"}" )&lt;BR /&gt;}&lt;BR /&gt;def json = JsonOutput.toJson(final1)&lt;BR /&gt;outputStream.write(JsonOutput.prettyPrint(json).getBytes(StandardCharsets.UTF_8))&lt;BR /&gt;} as StreamCallback)&lt;BR /&gt;&lt;BR /&gt;session.transfer(flowFile, REL_SUCCESS)&lt;BR /&gt;} catch(Exception e) {&lt;BR /&gt;log.error('Error during JSON operations', e)&lt;BR /&gt;flowFile = session.putAttribute(flowFile, "error", e.getMessage())&lt;BR /&gt;session.transfer(flowFile, REL_FAILURE)&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;Can you please help me with the same.&lt;/P&gt;&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 11 Jan 2023 09:21:18 GMT</pubDate>
    <dc:creator>Techie123</dc:creator>
    <dc:date>2023-01-11T09:21:18Z</dc:date>
    <item>
      <title>Getting Empty Array Using Groovy Script in  Nifi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Getting-Empty-Array-Using-Groovy-Script-in-Nifi/m-p/361014#M238494</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have an requirement where I need to parse the data into the required format&lt;/P&gt;&lt;P&gt;Input:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"Message" : "\nRecord 1:\nRequired data is missing. \n\nRecord 2:\nprocessing failed\n"&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;desired Output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;[{&lt;BR /&gt;"Record 1": "nRequired data is missing"&lt;BR /&gt;}, {&lt;BR /&gt;"Record 2": "processing failed"&lt;BR /&gt;}]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have written Groovy Script for the same but I am getting empty array.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import org.apache.commons.io.IOUtils&lt;BR /&gt;import groovy.json.*&lt;BR /&gt;import java.util.ArrayList&lt;BR /&gt;import java.nio.charset.*&lt;BR /&gt;import java.nio.charset.StandardCharsets&lt;BR /&gt;import groovy.json.JsonSlurper&lt;BR /&gt;import groovy.json.JsonBuilder&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;def flowFile = session.get()&lt;BR /&gt;if(!flowFile) return&lt;BR /&gt;try {&lt;BR /&gt;flowFile = session.write(flowFile,&lt;BR /&gt;{ inputStream, outputStream -&amp;gt;&lt;BR /&gt;def text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)&lt;BR /&gt;splitted = text.split('\nRecord')&lt;BR /&gt;int j = splitted.size()&lt;BR /&gt;final1 = []&lt;BR /&gt;for (int i=0;i&amp;lt;j-1;i++)&lt;BR /&gt;{&lt;BR /&gt;k = "Record " + splitted[i+1]&lt;BR /&gt;valid = k.replaceAll("\\n|\"|\\n|}","")&lt;BR /&gt;final1.add("{\"" + valid.replaceFirst(":",'":"')+ "\"}" )&lt;BR /&gt;}&lt;BR /&gt;def json = JsonOutput.toJson(final1)&lt;BR /&gt;outputStream.write(JsonOutput.prettyPrint(json).getBytes(StandardCharsets.UTF_8))&lt;BR /&gt;} as StreamCallback)&lt;BR /&gt;&lt;BR /&gt;session.transfer(flowFile, REL_SUCCESS)&lt;BR /&gt;} catch(Exception e) {&lt;BR /&gt;log.error('Error during JSON operations', e)&lt;BR /&gt;flowFile = session.putAttribute(flowFile, "error", e.getMessage())&lt;BR /&gt;session.transfer(flowFile, REL_FAILURE)&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;Can you please help me with the same.&lt;/P&gt;&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jan 2023 09:21:18 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Getting-Empty-Array-Using-Groovy-Script-in-Nifi/m-p/361014#M238494</guid>
      <dc:creator>Techie123</dc:creator>
      <dc:date>2023-01-11T09:21:18Z</dc:date>
    </item>
    <item>
      <title>Re: Getting Empty Array Using Groovy Script in  Nifi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Getting-Empty-Array-Using-Groovy-Script-in-Nifi/m-p/373186#M241517</link>
      <description>&lt;P&gt;This is Groovy code that achieves what you've mentioned and should be able to adapt to your scripted processor.&lt;/P&gt;&lt;LI-CODE lang="java"&gt;import groovy.json.JsonOutput
import groovy.json.JsonSlurper

JsonSlurper jsonSlurper = new JsonSlurper()
JsonOutput jsonOutput = new JsonOutput()

Map&amp;lt;String, String&amp;gt; data = jsonSlurper.parseText('''{
  "Message": "\nRecord 1:\nRequired data is missing. \n\nRecord 2:\nprocessing failed\n"
}''')

println(data)
println("=" * 80)

List&amp;lt;String&amp;gt; records = data.Message.split("\\nRecord\\s\\d+:\\n").findAll { it.trim() != "" }
println(records)
println("=" * 80)

Map&amp;lt;String, String&amp;gt; messages = [:]
records.eachWithIndex { message, index -&amp;gt;
    messages["Record ${index + 1}"] = message.trim()
}

println(jsonOutput.toJson(messages))&lt;/LI-CODE&gt;&lt;P&gt;This is the output:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[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"}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Jun 2023 00:18:47 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Getting-Empty-Array-Using-Groovy-Script-in-Nifi/m-p/373186#M241517</guid>
      <dc:creator>joseomjr</dc:creator>
      <dc:date>2023-06-25T00:18:47Z</dc:date>
    </item>
  </channel>
</rss>

