Groovy adds Iterable.collate(int size)
method via DefaultGroovyMethods
class and you can use it to split your input array into n-size chunks, e.g.
['a','b','c','d','e'].collate(3) == [['a','b','c'], ['d','e']]
Consider following example:
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
final String text = '[{"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}, {"id": 5}, {"id": 6}]'
final List<Map> json = new JsonSlurper().parseText(text) as List<Map>
json.collate(2).each { part ->
final String out = JsonOutput.toJson(part)
println "Sending following JSON object: ${out}"
}
Run in Groovy web console
Here we have a JSON array of 6 objects. We parse this JSON to a List<Map>
object and then we split into chunks of size 2 and prepare JSON for later execution. I used only 6 objects as an illustration, however it doesn't matter if the initial list contains 100 objects and we split into chunks of size 10 - the algorithm is the same.
It can be generalized and described in following steps:
- Parse initial JSON array of objects
- Split an array into chunks of size 10
- Format chunk of 10 objects into JSON String
- Process JSON document
The example shown above produces following output:
Sending following JSON object: [{"id":1},{"id":2}]
Sending following JSON object: [{"id":3},{"id":4}]
Sending following JSON object: [{"id":5},{"id":6}]