Groovy web console

subscribe to the feed Subscribe
to this
site

GroovyTest

Published 1 month ago by Manuel Echeverry with tags 1
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 == product.id }
        .inject(0){total, it -> 0 + it.quantity}

    def total = orders_amount * Float.parseFloat(product.price);

    printf('|%14s|%9s|%19s|\n', product.name, orders_amount, total )
    println "+--------------+---------+-------------------+";
}