How to extract data from a response and add them together?

I’m trying to take my response and extract certain values from the body. I’d like to add them all up to verify what the total should be.

For example:

dataforJohn: {


So I’d like to essentially extract the values of ‘grade’ and total them up through script and log it into the console and validate that I have the correct sum of the totalGrade.
If I have the wrong total for the grades, I’d also like to log that into the console.

Hi @material-candidate-4

Your response will be stored in pm.response variable. If it is a JSON response you can use JSON parser to convert to js object and then iterate through each grade value and add that. Pseudo logic will be like that.

Rest how the code will be written, that depends on the structure of response.

Hope this helps for ur query

@material-candidate-4 Welcome to the community :bouquet:

Are you sure about the response JSON? Since it has duplicate object keys. Else the format should be wrong. But the process will splitting up the values and store it in a array. And then you can use reduce() in js to calculate the sum of the array. Then you need to store the total in a variable and verify against it.

But again all these depends on the format of your response :slight_smile: