You can think of another approach which is using HashMap in groovy. This will make it easier to set the key and value corresponding to each other and hence writing it to datasink.
String jsonString = "your response as JSON"
HashMap<String,List<String>> myMap = new HashMap<String,List<String>>();
try {
JSONObject json = new JSONObject(jsonString);
JSONArray dataArray = json.getJSONArray("personalizedOffers");
for(int i = 0; i < dataArray.length(); i++) {
JSONObject tagObject = dataArray.getJSONObject(i);
//Write code to pass till to reach "promotion" object. since, "categoryType" and "id" exists inside "promotion" object
String category = tagObject.getString("categoryType");
String id = tagObject.getString("id");
if(myMap.containsKey(id)) {
List<String> arrayID = new ArrayList<String>();
arrayID.add(id);
myMap.put(category, arrayID);
}
This way, you can assign each "id" corresponding to "categoryType" and then write all together using for loop to DataSink.
This is a bot of tricky aproach and also please go through JSON manipulation. How to handle JSON.
https://www.journaldev.com/2321/gson-example-tutorial-parse-json