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 on 11-29-2022 01:21 AM - edited 11-29-2022 01:46 AM
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.
Created 06-24-2023 05:55 PM
First, the incoming Data should be proper JSON that can be easily parsed and processed...i.e. something like this
[
[
{
"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"
}
]
}
]
Then Groovy code like this:
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
JsonSlurper jsonSlurper = new JsonSlurper()
JsonOutput jsonOutput = new JsonOutput()
List data = jsonSlurper.parseText('''
[
[
{
"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"
}
]
}
]
''')
println(data)
println("=" * 80)
List<Map<String, String>> transformedList = []
data[0].eachWithIndex { map, index ->
Map<String, Object> record = data[1]["res_data"].find { it["Record"] == "Record ${index + 1}" }
if (record) {
map.putAll(record)
}
transformedList.add(map)
}
println(jsonOutput.prettyPrint(jsonOutput.toJson(transformedList)))
Creates an output like this:
[[[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]]]]
================================================================================
[
{
"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",
"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"
}
]