Ask a Question

Groupby using groovy

SOLVED
SK07
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
nmrao
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.

View solution in original post

5 REPLIES 5
nmrao
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.

View solution in original post

SK07
Contributor

Re: Groupby using groovy

Thank you!Rao.

SK07
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.

HimanshuTayal
Community Hero

Re: Groupby using groovy

any example what you exactly want to do.


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
SK07
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

cancel
Showing results for 
Search instead for 
Did you mean: