Groovy web console

subscribe to the feed Subscribe
to this
site

Products

Published 1 month ago by Felipe Martinez with tags tailorsoft
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.json.JsonSlurper
 
def url_Json = new URL("https://www.tailorsoft.co/sample.json")

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

def getJson = new JsonSlurper().parseText(url_Json.text)
def getOrders = getJson.orders.items.flatten()
getJson.products.each { prod -> 
    (getOrders.findAll { it.productId == prod.id }.sum { it.quantity } ?: 0).with { o ->
        println '| ' + prod.name.padRight(13) + '| ' + o.toString().center(8) + '|' +
                     (new BigDecimal(prod.price) * o).toString().padLeft(11) + ' |' 
    }
    println "+--------------+---------+------------+"
}
println "";