Hello everyone,
I’m passing a request body with a JSON object containing n number of fields.
The body format is included in the body and this specific JSON object is in the pre-requisite script.
Upon hitting the post request an ID gets generated.
Now, I use get API with this ID to fetch the response.
Till now it’s all good.
As part of the test, I need to validate the response of the get API containing the JSON node that is passed as part of the prerequisite script.
methods that I have tried:
prerequisite step -
var employeedetails = { “id” = “12”, “location” = “USA”}
pm.expect(pm.response.text()).to.include(employeedetails)
this fails because in the response body, the employeedetails JSON object displays the fields in a different order like
response
{
“body”: xxxxx,
“employeedetails”:
{
“location” = “USA”,
“id” = “12”,
}
}
the second method that I have tried is
extracting this specific node from the response and comparing it with the node passed in the request.
like
var jsonData = JSON.parse(responsebody);
var response = jsonData.employeedetails;
var responseNode = JSON.stringify(response)
._equals (employeedetails, responseNode)
this returns true even when the fields are missing/different.
What I’m looking for is
the comparison should verify the count of elements in the request vs response matching
the key-value pairs are matching, regardless of the order in the request vs response
only when these conditions are true, does the function have to return true.
Your help is highly appreciated!!