Requirement: To sum up all the "availableAmount" values in the below JSON and store into a variable.
JSON:
[{
"id": 0,
"availableAmount": 100.55
},
{
"id": 0,
"availableAmount": 200.87
},
{
"id": 0,
"availableAmount": 357.54
}]
My Approach:
1. Use JSON Extractor "$[*].availableAmount" to get an array of values and store them to a correlated variable.Here, the wildcard extracts all of the matching values. So the varaible will contain values [100.55,200.87,357.54]
2. Store the above array of values individually to separate variables. eg. a=100.55, b=200.87, c=357.54
3. Store the sum of a,b and c to another variable, i.e. d=a+b+c
How to perform Steps 2 and 3 in NeoLoad? Please provide suggestions if there is a better approach.
Hi,
This could be done with some Javascript in Neoload, here is some pseudo code
var myArray = {"1","2","3"};
var total = parseInt(myArray[0]) + parseInt(myArray[1]) +parseInt(myArray[2])
The syntax might need some work but this should help
A challenge here is that the JSON response is dynamic in nature and the number of array values could vary. Also, the amount will always be float values.
The right regex all always get the list of amounts into the array, you can then just loop around the size of the array adding the values up. You can use parseFloat
for (var count=0;count < myArray.len; count++) {
total = parseFloat(total) + parseFloat(myArray[count]) }