Reto E.
Reto E.

Reputation: 119

groovy sum of array (with string conversion)

I have a JSON response from a REST post. Now I want to get the sum of a field.

restResponse*.amount.sum()

But the amount field is a string so it just concatenats those strings ...

Is there a way to convert the strings 'on the fly' to float or double for .sum()?

Upvotes: 0

Views: 705

Answers (1)

Om Prakash
Om Prakash

Reputation: 853

Try as below :-

 //sum in integer
 restResponse*.amount*.toInteger().sum()

 //sum in double
 restResponse*.amount*.toDouble().sum()

 //sum in float
 restResponse*.amount*.toFloat().sum()

Hope it will help you...:)

Upvotes: 2

Related Questions