<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question How to loop through array of json object and rebuilt the json array using Groovy script in Apache NiFi in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/How-to-loop-through-array-of-json-object-and-rebuilt-the/m-p/358535#M237872</link>
    <description>&lt;P&gt;Hi Team,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have below requirement,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my flow file contains below data:&lt;/P&gt;&lt;P&gt;[ {&lt;BR /&gt;{&lt;BR /&gt;"ID" : "100000",&lt;BR /&gt;"Date" : "2022-09-22",&lt;BR /&gt;"Start Time" : "08:00",&lt;BR /&gt;"End Time" : "14:00",&lt;BR /&gt;&lt;BR /&gt;}, {&lt;BR /&gt;"ID" : "100001",&lt;BR /&gt;"Date" : "2022-09-02",&lt;BR /&gt;"Start Time" : "08:00",&lt;BR /&gt;"End Time" : "14:00",&lt;BR /&gt;},&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"ID" : "100002",&lt;BR /&gt;"Date" : "2022-09-02",&lt;BR /&gt;"Start Time" : "08:00",&lt;BR /&gt;"End Time" : "14:00",&lt;BR /&gt;}&lt;BR /&gt;} ]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;[ {"res_data" : {&lt;BR /&gt;"Record" : "Record 2",&lt;BR /&gt;"Description" : "Invalid values for ID or Date"&lt;BR /&gt;},&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"Record" : "Record 3",&lt;BR /&gt;"Description" : "Invalid values for ID or Date"&lt;BR /&gt;}} ]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the requirement is:&lt;/P&gt;&lt;P&gt;if concatenate ('Record' with index of array of json object ==&amp;nbsp;res_data.Record&amp;nbsp;&lt;BR /&gt;then it should return&lt;BR /&gt;{&lt;BR /&gt;{&lt;BR /&gt;"ID" : "100001",&lt;BR /&gt;"Date" : "2022-09-02",&lt;BR /&gt;"Start Time" : "08:00",&lt;BR /&gt;"End Time" : "14:00",&lt;/P&gt;&lt;P&gt;"Record" : "Record 2",&lt;BR /&gt;"Description" : "Invalid values for ID or Date"&lt;BR /&gt;}&lt;BR /&gt;} and so on&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;else&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"ID" : "100000",&lt;BR /&gt;"Date" : "2022-09-02",&lt;BR /&gt;"Start Time" : "08:00",&lt;BR /&gt;"Time Out" : "14:00",&lt;BR /&gt;"End Time" : "14:00",&lt;/P&gt;&lt;P&gt;"Description": null&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and the final output looks like&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;[&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"ID" : "100001",&lt;BR /&gt;"Date" : "2022-09-02",&lt;BR /&gt;"Start Time" : "08:00",&lt;BR /&gt;"End Time" : "14:00",&lt;/P&gt;&lt;P&gt;"Record" : "Record 2",&lt;BR /&gt;"Description" : "Invalid values for ID or Date"&lt;BR /&gt;},&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"ID" : "100002",&lt;BR /&gt;"Date" : "2022-09-02",&lt;BR /&gt;"Start Time" : "08:00",&lt;BR /&gt;"End Time" : "14:00",&lt;/P&gt;&lt;P&gt;"Record" : "Record 3",&lt;BR /&gt;"Description" : "Invalid values for ID or Date"&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;]&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;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.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 29 Nov 2022 09:46:51 GMT</pubDate>
    <dc:creator>Techie123</dc:creator>
    <dc:date>2022-11-29T09:46:51Z</dc:date>
    <item>
      <title>How to loop through array of json object and rebuilt the json array using Groovy script in Apache NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-loop-through-array-of-json-object-and-rebuilt-the/m-p/358535#M237872</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have below requirement,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my flow file contains below data:&lt;/P&gt;&lt;P&gt;[ {&lt;BR /&gt;{&lt;BR /&gt;"ID" : "100000",&lt;BR /&gt;"Date" : "2022-09-22",&lt;BR /&gt;"Start Time" : "08:00",&lt;BR /&gt;"End Time" : "14:00",&lt;BR /&gt;&lt;BR /&gt;}, {&lt;BR /&gt;"ID" : "100001",&lt;BR /&gt;"Date" : "2022-09-02",&lt;BR /&gt;"Start Time" : "08:00",&lt;BR /&gt;"End Time" : "14:00",&lt;BR /&gt;},&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"ID" : "100002",&lt;BR /&gt;"Date" : "2022-09-02",&lt;BR /&gt;"Start Time" : "08:00",&lt;BR /&gt;"End Time" : "14:00",&lt;BR /&gt;}&lt;BR /&gt;} ]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;[ {"res_data" : {&lt;BR /&gt;"Record" : "Record 2",&lt;BR /&gt;"Description" : "Invalid values for ID or Date"&lt;BR /&gt;},&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"Record" : "Record 3",&lt;BR /&gt;"Description" : "Invalid values for ID or Date"&lt;BR /&gt;}} ]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the requirement is:&lt;/P&gt;&lt;P&gt;if concatenate ('Record' with index of array of json object ==&amp;nbsp;res_data.Record&amp;nbsp;&lt;BR /&gt;then it should return&lt;BR /&gt;{&lt;BR /&gt;{&lt;BR /&gt;"ID" : "100001",&lt;BR /&gt;"Date" : "2022-09-02",&lt;BR /&gt;"Start Time" : "08:00",&lt;BR /&gt;"End Time" : "14:00",&lt;/P&gt;&lt;P&gt;"Record" : "Record 2",&lt;BR /&gt;"Description" : "Invalid values for ID or Date"&lt;BR /&gt;}&lt;BR /&gt;} and so on&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;else&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"ID" : "100000",&lt;BR /&gt;"Date" : "2022-09-02",&lt;BR /&gt;"Start Time" : "08:00",&lt;BR /&gt;"Time Out" : "14:00",&lt;BR /&gt;"End Time" : "14:00",&lt;/P&gt;&lt;P&gt;"Description": null&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and the final output looks like&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;[&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"ID" : "100001",&lt;BR /&gt;"Date" : "2022-09-02",&lt;BR /&gt;"Start Time" : "08:00",&lt;BR /&gt;"End Time" : "14:00",&lt;/P&gt;&lt;P&gt;"Record" : "Record 2",&lt;BR /&gt;"Description" : "Invalid values for ID or Date"&lt;BR /&gt;},&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"ID" : "100002",&lt;BR /&gt;"Date" : "2022-09-02",&lt;BR /&gt;"Start Time" : "08:00",&lt;BR /&gt;"End Time" : "14:00",&lt;/P&gt;&lt;P&gt;"Record" : "Record 3",&lt;BR /&gt;"Description" : "Invalid values for ID or Date"&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;]&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;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.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2022 09:46:51 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-loop-through-array-of-json-object-and-rebuilt-the/m-p/358535#M237872</guid>
      <dc:creator>Techie123</dc:creator>
      <dc:date>2022-11-29T09:46:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to loop through array of json object and rebuilt the json array using Groovy script in Apache NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-loop-through-array-of-json-object-and-rebuilt-the/m-p/373187#M241518</link>
      <description>&lt;P&gt;First, the incoming Data should be proper JSON that can be easily parsed and processed...i.e. something like this&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;[
	[
		{
			"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"
			}
		]
	}
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then Groovy code like this:&lt;/P&gt;&lt;LI-CODE lang="java"&gt;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&amp;lt;Map&amp;lt;String, String&amp;gt;&amp;gt; transformedList = []

data[0].eachWithIndex { map, index -&amp;gt;
    Map&amp;lt;String, Object&amp;gt; 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)))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Creates an output like this:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[[[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"
    }
]&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 25 Jun 2023 00:55:53 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-loop-through-array-of-json-object-and-rebuilt-the/m-p/373187#M241518</guid>
      <dc:creator>joseomjr</dc:creator>
      <dc:date>2023-06-25T00:55:53Z</dc:date>
    </item>
  </channel>
</rss>

