Groovy web console

subscribe to the feed Subscribe
to this
site

Groovy test

Published 3 weeks ago by Adrian Baldovino
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.json.JsonSlurper

def urlJson = new URL("https://www.tailorsoft.co/sample.json")
def jsonContent = new JsonSlurper().parse(urlJson)

def productList = [:]
def productValues = {}

jsonContent.products.each{
    productValues = [product: it, ocurrences:0, total:0]
    productList.put(it.id, productValues)
}

for (order in jsonContent.orders){
    for (item in order.items){
        productValues = productList[item.productId]
        productValues.ocurrences++
        productValues.total+= productValues.product.price.toDouble() * item.quantity.toDouble()
        productList.put(item.productId, productValues)
    }
}

println "+--------------+---------+------------+";
println "| Product      |  Orders |      Total |";
println "+--------------+---------+------------+";

productList.each {
  println "${it.value.product.name} \t\t ${it.value.ocurrences} \t\t ${it.value.total}"
}