Forum Discussion

SK07's avatar
SK07
Contributor
7 years ago

Json response for inner loop

Json : {"prods": [{"Item":11,"rate": 100, "Ids": [ {"level": 1}]},     {"Item":12,"rate":200, "Ids": [ {"level": 2}, {"level": 2}, {"level": 2}]},     {"Item":13,"rate":200}, "Ids": [ {"level": 1}]]}...
  • aaronpliu's avatar
    aaronpliu
    7 years ago

    Hi SK07,

     

    Getting your point and give u a solution below:

    //YourJsonData
    def data = new groovy.json.JsonSlurper().parseText(YourJsonData)
    def aa = []
    data.prods.each{id ->
        aa << id.ids.collect{le ->
            le.level
            }
    }
    
    def bb = aa.flatten()
    aa.flatten().unique().each{a ->
        def count = 0
        for(b in bb){
            if(b == a) count++
            }
        // save your value if need
        log.info "[$a:$count]"
    }

    Hopefully above example can resolve your requirement.

     

     

    Thanks,

    /Aaron