def list = [
[id: 1, name: 'John'],
[id: 2, name: 'Jane'],
[id: 3, name: 'Alice'],
[id: 1, name: 'Bob'],
[id: 1, name: 'Hank'],
[id: 2, name: 'Mary']
]
def groupedMap = list.groupBy { it.id }
def pcList = new ArrayList()
groupedMap.each { key, value ->
def chunkedList = value.collate(2)
chunkedList.eachWithIndex { sublist, index ->
def mp = new HashMap()
mp.put("method", "insertDrugInfo")
mp.put("opter_id", sublist.get(0).get("id"))
mp.put("opter_name", sublist.get(0).get("name"))
mp.put("fixmedins_code","P00000000001")
def newSubList = new ArrayList()
for(mmp in sublist){
mmp = mmp.collectEntries { k, v ->
[(k == 'id' ? 'phone' : k): v]
}
newSubList.add(mmp)
}
mp.put("detail", newSubList);
pcList.add(mp)
}
}
pcList.each { map ->
println map
}