Groovy web console

subscribe to the feed Subscribe
to this
site

GroovyScriptTestFix

Published 1 month ago by Manuel Fernando with tags 2
Actions  ➤ 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")
def table = new JsonSlurper().parse(url_Json)
def all_items = table.orders.collect{ it.items }.flatten()
 
// Print the table header
println "+--------------+---------+-------------------+";
println "| Product      |  Orders |      Total        |";
println "+--------------+---------+-------------------+";
 
table.products.each{ product ->
    def orders_amount = all_items.findAll{ it.productId.equals(product.id) }
        .inject(0){t, it -> t + it.quantity}
 
    def total = orders_amount * Float.parseFloat(product.price);
 
    printf('|%14s|%9s|%19s|\n', product.name, orders_amount, total )
    println "+--------------+---------+-------------------+";
}