Testing multiple JSON lines response

3.5k Views Asked by At

I am trying to make a test in Postman to verify some content in a JSON response. If I just try to verify a single line from the JSON response everything is fine. My problem starts when I need to test multiple lines of the JSON response. Is always failing. Any suggestion?

tests["Body matches string"] = responseBody.has("\"name\": null,
                \"nameType\": \"NON_REFUNDABLE\"");
1

There are 1 best solutions below

2
On

If I understand your question correctly I'd like to suggest that you approach this in a different way.

Instead of looking at the entire response body and seeing if the strings match you could alternatively test the individual Json properties that make up the response body. For example you could do the following:

var data = JSON.parse(responseBody);

tests["name is null"] = data.name === null;
tests["nameType is non-refundable"] = data.nameType === "NON_REFUNDABLE";

There are other alternatives as well but this is the first that comes to mind. For some more ideas about testing using postman check out their documentation and examples.