Groovy web console

subscribe to the feed Subscribe
to this
site

groovy_solution_IR

Published 4 weeks ago by Isaura Ruiz
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.json.JsonSlurper;
import groovy.json.JsonBuilder;

def url_Json = new URL("https://www.tailorsoft.co/sample.json").openStream()     
def rd = new InputStreamReader(url_Json, "UTF-8"); 
def jsonText =rd.getText();
def jsonSlurper = new JsonSlurper();
def parsedJson =jsonSlurper.parseText(jsonText);

//parsedJson.each{entry -> println "$entry.key: $entry.value"}

//recorro lo que obtuve al parsear el json para validar lo que obtuve
 /*for (elementos in parsedJson.keySet()) {
   println(elementos)
}*/
//obtengo los valores para generar 2 listas
my_keys = parsedJson.values()
lista = my_keys as ArrayList
productos = lista[0];
ordenes=lista[1];
def idP;
def nombre;
def precio;


// Print the table header
println "+--------------+---------+------------+";
println "| Product      |  Orders |      Total |";
println "+--------------+---------+------------+";

//logica para cumplir con lo solicitado
for (prod in productos){
 idProd= prod.id;
nombre= prod.name;
precio = prod.price;
 for (ord in ordenes){
  ord.items.each{   
    if (it.productId == idProd){
      println prod.name+'       '+ ord.id+'     '+prod.price;
  }
      }
  }
}