Groovy web console

subscribe to the feed Subscribe
to this
site

JSON Parser TailorSoft

Published 3 weeks ago by John Dederle
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");
def parsedResponse = new JsonSlurper().parseText(url_Json.text);
def allProducts = parsedResponse.products;
def allOrders = parsedResponse.orders;

// Print the table header
println "+--------------+---------+------------+";
println "| Product      |  Orders |      Total |";
println "+--------------+---------+------------+";
                         
allOrders.each { x ->
    x.items.each { i ->
        Double total = 0;
        int ordersCount = 0;
        allProducts.findAll { v -> v.id == i.productId; }.each { items ->
            ordersCount++;
            total += Double.parseDouble(items.price)
            println "|${items.name}   |" + ordersCount + "|" + total
            println "+--------------+---------+------------+"
        }
    }
}