Subtract a value from a response

Can someone please help me, I have a response that has a value=1000 I want to take it and subtract 100 for example so: 1000-100=900 and set it as a variable?

Maybe something like:

var jsonData = JSON.parse(responseBody);

Hey @parsy2 :wave:

Can you share an example of the response body please?

It will make it easier for someone to offer a solution.

1 Like

Hi @parsy2 and @dannydainton :wave:t4:

I agree that it may be easier if we get an example of your response body, however in the meantime let me show you an example of how you could go about doing this using Valentin Despa ‘s API Documentation examples.

I will be playing with this Order Pizza API


This text will be hidden

: Swagger UI

The response body for this Get Request is the following :

        "Crust": "NORMAL",
        "Flavor": "BEEF-NORMAL",
        "Order_ID": 1,
        "Size": "M",
        "Table_No": 1,
        "Timestamp": "2019-12-03T18:21:08.669365"
        "Crust": "THIN",
        "Flavor": "CHEESE",
        "Order_ID": 2,
        "Size": "S",
        "Table_No": 5,
        "Timestamp": "2019-12-03T18:21:08.708470"
        "Crust": "NORMAL",
        "Flavor": "CHICKEN-FAJITA",
        "Order_ID": 3,
        "Size": "L",
        "Table_No": 3,
        "Timestamp": "2019-12-03T18:21:08.710006"

This is how I would go about parsing this response body, subtracting a number and adding it to an environment variable.

The first thing you would need to do is parse the correct object that you are interested in by index. In my case, I am interested in the “Table_No” value in index 1, which is a value of 5.

var jsonData = JSON.parse(responseBody);
var x = jsonData[1].Table_No; 

Once I have that value, I can then go ahead and subtract it by whatever and then finally set the environment variable

var y = x - 2;
pm.environment.set("NewValue", y);

I have this all in a test to show you the result. Here I am expecting a value of 3.

As you can see, I have also created an environment variable, that takes the subtracted value when you send the request.

I hope this example helps you see how you can go about doing something similar with your use case.

If you are interested in condensing the script this would work as well:

var jsonData = JSON.parse(responseBody);
pm.environment.set("NewValue", (jsonData[1].Table_No)-2);