POSTMAN: Comparing object Environment variable with response's object

I am having a problem with comparing object typed Env variable with the response’s object in Postman even though it seems the same and couldn’t find answers anywhere.

Here’s the example:

The object used is:

  "id" = 1,
  "first_name": "John",
  "last_name": "Smith"

When using a POST request I save the object as Environment Variable using:

var reqdata = JSON.parse(data.request);
postman.setEnvironmentVariable("User", JSON.stringify(reqdata.user));

and then in a GET response I want to compare it by using:

Pre-request Script:

user = JSON.parse(postman.getEnvironmentVariable("User"));

and then in Tests:

var data = JSON.parse(responseBody);
tests["user contains correct data"] = data.user == user;

The console.log returns exactly the same objects but I am still getting fail. I tried using and === but it still returns fail. Could somebody please tell me what I am missing?


46 thoughts on “POSTMAN: Comparing object Environment variable with response's object”

Leave a Comment