Groovy web console

subscribe to the feed Subscribe
to this
Convert List of Map to JSON (via #groovywebconsole)
tweet this snippet Tweet

Convert List of Map to JSON

Published 10 months ago by Nitin Dhomse with tags groovy json map list
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts

List list = [[field:"A", amount:1000], [field:"A", amount:2000], [field:"B", amount:3000], [field:"C", amount:4000]]
println "Input: "+list
def json  = groovy.json.JsonOutput.toJson(list.groupBy{it.field}.collect{['field':it.key, 'amount':it.value.sum{it.amount}]})
println "Output: "+ json