How can Postman compare the value of a written variable with the value from json?

The “id_user” variable has the value “3”. the “id” from json is equal to “3”.

pm.expect(json Data.id).to.eql("{{id_user}}");

Using this command, I try to compare values. Returns an error:

AssertionError: expected 3 to deeply equal ‘{{id_user}}’

If the code above is replaced with:

pm. expert(json Data.id).to. eql(3);

Everything works.

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Hey @alexei.taushkanov

The {{...}} syntax isn’t available in the sandbox to use directly in the expect statement like that.

You would need to use it like this:

pm.expect(jsonData.id).to.eql(pm.environment.get("id_user"));

Alternatively, that double curly brace syntax could be used like this:

pm.expect(jsonData.id).to.eql(pm.environment.replaceIn("{{id_user}}"));

2 Likes

@dannydainton
Returns an error: Your test name | AssertionError: expected 3 to deeply equal ‘3’. =(
Is it possible that the value “id_user” was written in string format?

That can be handled like this, it will turn the variable saved as a string into an integer.

pm.expect(jsonData.id).to.eql(parseInt(pm.environment.get("id_user")));

2 Likes