Groovy web console

subscribe to the feed Subscribe
to this
site

formatJsonGridValue

Published 7 days ago by gcespedes
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def lstToJson(List lstGridValue) {
    def generator = new groovy.json.JsonGenerator.Options().build()
    def builder = new groovy.json.JsonBuilder(generator)
    
    builder lstGridValue
    
    return builder.toPrettyString()
}

def formatJsonRapipagoGridValue(String strJsonRapipagoGridValue)
{   
    def mapItemGridValue = [:]
    def lstGridValue = []

    def jsonRapipagoGridValue = new groovy.json.JsonSlurper().parseText(strJsonRapipagoGridValue)
 
    jsonRapipagoGridValue.valoresGrilla.each {
        mapItemGridValue = [:]
        it.each {
            mapItemGridValue.put(it.codCampo, it.valor)
        }
        lstGridValue.add(mapItemGridValue)
    }
    return lstToJson(lstGridValue)
}

def respValoresGrilla = '{"valoresGrilla":[[{"codCampo":"barra","valor":"4444"},{"codCampo":"NRF","valor":"000000000000170980000620120617"},{"codCampo":"DAF","valor":""},{"codCampo":"anonimo0","valor":"8"},{"codCampo":"anonimo2","valor":"00006"},{"codCampo":"anonimo3","valor":"100"},{"codCampo":"anonimo4","valor":"17/06/2012"},{"codCampo":"anonimo1","valor":"0000000000001709"},{"codCampo":"codTI","descCampo":"ESC"},{"codCampo":"descTI","descCampo":"Cobranza cerrado"},{"codCampo":"a_pagar","valor":"100"}],[{"codCampo":"barra","valor":"4353000000000000170980000620120617000000000100000400023"},{"codCampo":"NRF","valor":"000000000000170980000620120617"},{"codCampo":"DAF","valor":""},{"codCampo":"anonimo0","valor":"8"},{"codCampo":"anonimo2","valor":"00006"},{"codCampo":"anonimo3","valor":"100"},{"codCampo":"anonimo4","valor":"17/06/2012"},{"codCampo":"anonimo1","valor":"0000000000001709"},{"codCampo":"codTI","descCampo":"ESC"},{"codCampo":"descTI","descCampo":"Cobranza cerrado"},{"codCampo":"a_pagar","valor":"100"}]]}'
println formatJsonRapipagoGridValue(respValoresGrilla)