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]
Created 12-01-2022 12:49 AM
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
Created on 12-02-2022 12:55 AM - edited 12-02-2022 02:38 AM
@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)
}
Created 12-01-2022 10:23 PM
Hi Team, can you please help.
Created on 12-02-2022 12:55 AM - edited 12-02-2022 02:38 AM
@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)
}
Created 12-06-2022 04:10 AM
Thank you so much @Faerballert