Support Questions

Find answers, ask questions, and share your expertise

How to get pretty array of JSON as groovy Script Output in NiFi

avatar
Contributor

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

1 ACCEPTED SOLUTION

avatar
Expert Contributor

@Techie123 maybe you can beautify it a bit 😄 

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 content = new JsonSlurper().parseText(text)

      for (int i = content.size() - 1; i >= 0; --i) {
        int objNo = i
        int recNo = i + 1
        def rec = 'Record ' + recNo

        if (rec != content[objNo].enrichment.Record) {
          content.remove(content[objNo])
        }
      }
      def jsonOutput = JsonOutput.toJson(content)
      outputStream.write(JsonOutput.prettyPrint(jsonOutput).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)
}

 

View solution in original post

3 REPLIES 3

avatar
Contributor

Hi Team, can you please help.   

avatar
Expert Contributor

@Techie123 maybe you can beautify it a bit 😄 

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 content = new JsonSlurper().parseText(text)

      for (int i = content.size() - 1; i >= 0; --i) {
        int objNo = i
        int recNo = i + 1
        def rec = 'Record ' + recNo

        if (rec != content[objNo].enrichment.Record) {
          content.remove(content[objNo])
        }
      }
      def jsonOutput = JsonOutput.toJson(content)
      outputStream.write(JsonOutput.prettyPrint(jsonOutput).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)
}

 

avatar
Contributor

Thank you so much @Faerballert