Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

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

avatar
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.  

1 REPLY 1

avatar
Super Collaborator

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"
    }
]