Support Questions

Find answers, ask questions, and share your expertise

How to loop through array of json object and rebuilt the json array using Groovy script in Apache NiFi

Explorer

Hi Team,

 

I have below requirement,

 

my flow file contains below data:

[ {
{
"ID" : "100000",
"Date" : "2022-09-22",
"Start Time" : "08:00",
"End Time" : "14:00",

}, {
"ID" : "100001",
"Date" : "2022-09-02",
"Start Time" : "08:00",
"End Time" : "14:00",
},

{
"ID" : "100002",
"Date" : "2022-09-02",
"Start Time" : "08:00",
"End Time" : "14:00",
}
} ]

 

[ {"res_data" : {
"Record" : "Record 2",
"Description" : "Invalid values for ID or Date"
},

{
"Record" : "Record 3",
"Description" : "Invalid values for ID or Date"
}} ]

 

And the requirement is:

if concatenate ('Record' with index of array of json object == res_data.Record 
then it should return
{
{
"ID" : "100001",
"Date" : "2022-09-02",
"Start Time" : "08:00",
"End Time" : "14:00",

"Record" : "Record 2",
"Description" : "Invalid values for ID or Date"
}
} and so on

 

else

{
"ID" : "100000",
"Date" : "2022-09-02",
"Start Time" : "08:00",
"Time Out" : "14:00",
"End Time" : "14:00",

"Description": null

}

 

and the final output looks like


[

{
"ID" : "100001",
"Date" : "2022-09-02",
"Start Time" : "08:00",
"End Time" : "14:00",

"Record" : "Record 2",
"Description" : "Invalid values for ID or Date"
},

{
"ID" : "100002",
"Date" : "2022-09-02",
"Start Time" : "08:00",
"End Time" : "14:00",

"Record" : "Record 3",
"Description" : "Invalid values for ID or Date"
}



]


I want to do it using groovy script but I don't know anything about the groovy script. Can you please help if anyone has done it before.  

0 REPLIES 0