cancel
Showing results for 
Search instead for 
Did you mean: 

Groupby using groovy

SOLVED
Highlighted
Contributor

Groupby using groovy

I want to display by groupby item and count the rate using groovy in ReadyAPI.(Like this: Item 12: Count(Rate) : 400

Json1 : {"prods": [{"Item":"11","rate": 100},{Item":"12","rate":200},{Item":"12","rate":200}]}. 

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: Groupby using groovy

@SK07,

 

Here you go:
https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/json/JsonGroupByAndSum.groovy

 

def json = new groovy.json.JsonSlurper().parseText(jsonString)
json.prods.groupBy{it.Item}.each { key, value -> 
	log.info "Item : ${key}, Sum (of rate): ${value.sum{it.rate}}" 
}


Regards,
Rao.
5 REPLIES 5
Community Hero

Re: Groupby using groovy

@SK07,

 

Here you go:
https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/json/JsonGroupByAndSum.groovy

 

def json = new groovy.json.JsonSlurper().parseText(jsonString)
json.prods.groupBy{it.Item}.each { key, value -> 
	log.info "Item : ${key}, Sum (of rate): ${value.sum{it.rate}}" 
}


Regards,
Rao.
Contributor

Re: Groupby using groovy

Thank you!Rao.

Contributor

Re: Groupby using groovy

I want to print  how many nulls, 1's and 2's seperately.

Json : {"prods": [{"Item":13,"rate":200, "Ids": []}, {"Item":13,"rate":200, "Ids": []},
{"Item":11,"rate": 100, "Ids": [ {"level": 1}]},
{"Item":13,"rate":200, "Ids": [ {"level": 1}]},
{"Item":12,"rate":200, "Ids": [ {"level": 2} ], [{"level": 2}, {"level": 2}]} ]}.

def jsonids = json.Ids
log.info jsonids
[[], [], [1], [1], [2, 2, 2]]

I tried and couldnt seperate...Any help is appreciated.

Regular Contributor

Re: Groupby using groovy

any example what you exactly want to do.


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal
Contributor

Re: json reading nulls

For that request,I wanted to get results like :
Level nulls : 2
Level one's : 2
Level two's : 3

New Here?
Join us and watch the welcome video:
Top Kudoed Authors