Verifying current response against Json response from Json data

Hello Friends,

I am facing an issue with verifying the json response stored in the Json data file with the actual response. I wanted to verify the propertyInvestmentRatios array alone from the response, for which I was getting an error, so I am trying to verify the entire response stored in CSV against my actual response received. Appreciate your help on this matter.

First of all below is my response which I have stored in a json file with the key as fullReponseBody

"fullResponseBody": {
      "entities": {
        "propertyInvestmentRatios": [
          {
            "id": "805432",
            "cashFlowAvailable": "-126391.19",
            "accessedInterestExpense": "691.31",
            "propertyICR": "-182.83",
            "groupSurplusRequired": "133304.29",
            "surplusAvailable": "-999.00",
            "assessmentResult": "Incomplete"
          }
        ]
      },
      "result": {
        "propertyInvestments": [
          "805432"
        ]
      }
    }

I am trying to compare by fetching the response but I am getting an error as below. The issue I notice is that the block of code is being read an an object.

Test 1 :- To verify the response as is from the CSV which is stored exactly the same.

pm.test("Verify the response for Property Investment Ratio Calculator", function () {
    var jsonResponse = pm.response.json();
    console.log('ID returned from Response is '+jsonResponse.entities.propertyInvestmentRatios[0]);
    pm.expect(pm.response.text()).to.contains((pm.iterationData.get("fullResponseBody")));
});

Below is the error I get in the console.

Verify the response for Property Investment Ratio Calculator | AssertionError: expected '{"entities":{"propertyInvestmentRatios":[{"id":"805432","cashFlowAvailable":"-126391.19","accessedInterestExpense":"691.31","propertyICR":"-182.83","groupSurplusRequired":"133304.29","surplusAvailable":"-999.00","assessmentResult":"Incomplete"}]},"result":{"propertyInvestments":["805432"]}}' to include { entities: { propertyInvestmentRatios: [ [Object] ]}, result: { propertyInvestments: [ '805432' ] } }

I tried the below approach as well.

Test 2

pm.test("Body is correct", function () {
    pm.response.to.have.body(pm.iterationData.get("fullResponseBody"));
});

Body is correct | AssertionError: expected response body json to equal { Object (entities, result) } but got { Object (entities, result) }

The issue I notice is that the block of code is being read an an object from Json Data file. Appreciate if you can let me know what am I doing wrong. Thanks in advance.

Hi @kishoreccaus. Welcome to the Postman community :postman_logo:

The first syntax does not look correct. Ideally, it should look something like

.to.includes and I’m not sure I have seen .to.contains used before but I may be wrong.

Secondly, '.includesand.bodyprimarily work with strings, with.bodychecking against the entire response body string and.includes` against a specific portion of it.

To compare a JSON with a response body, you can explore one of the following options.

  • Compare individual properties of the JSON with each other. That way, you can have multiple tests and include multiple assertions in those tests to test for specific JSON properties rather than for the entire JSON itself.
  • If you’re familiar with JSON Schema, Author a schema for the JSON and validate the response body against that schema using pm.response.to.have.jsonSchema(schema);.
    Here, you’re validating the structure of the JSON and not necessarily the values of each property. But my guess is that the values will be dynamic, so this approach still works.

Hello Gbadebo,

Thank you so much for your response. Appreciate it.

Contains is a valid method as I copied it from Postman code snippet feature

I am already comparing individual properties from Json response, but I want to compare the contents of the entire response and not the schema alone.

By the way , I tried using the includes method as below but I am still getting the same error.

pm.test(“Body matches string”, function () {
pm.expect(pm.response.text()).to.includes(pm.iterationData.get(“fullResponseBody”));
});

Body matches string | AssertionError: expected ‘{“entities”:{“propertyInvestmentRatios”:[{“id”:“805432”,“cashFlowAvailable”:“-126391.19”,“accessedInterestExpense”:“691.31”,“propertyICR”:“-182.83”,“groupSurplusRequired”:“133304.29”,“surplusAvailable”:“-999.00”,“assessmentResult”:“Incomplete”}]},“result”:{“propertyInvestments”:[“805432”]}}’ to include { entities: { propertyInvestmentRatios: [ [Object] ] }, result: { propertyInvestments: [ ‘805432’ ] } }

The problem I see is that the object under propertyInvestmentRatios array in the expected result from Json testdata file is being read as an object during the assertion.

You should store JSON data as strings (stringify) in collection or environment variables, and then PARSE when you retrieve.

This actually creates a JavaScript object and is not technically JSON at this point, although they look similar.

But the comparison should still work, as both JavaScript objects if parsed from the same JSON should be the same.

If you want to compare two JavaScript objects, then the assertion should be to.be.eql. Not contains.

It should also be pm.response.json(), not pm.response.text().

Instead of using (pm.iterationData.get(“fullResponseBody”) in the assertion. Define it as a variable and then console log it.

Define the expected and actual result as variables, and console log them both.

Both variables should end up looking like this.

{
    "entities": {
        "propertyInvestmentRatios": [
            {
                "id": "805432",
                "cashFlowAvailable": "-126391.19",
                "accessedInterestExpense": "691.31",
                "propertyICR": "-182.83",
                "groupSurplusRequired": "133304.29",
                "surplusAvailable": "-999.00",
                "assessmentResult": "Incomplete"
            }
        ]
    },
    "result": {
        "propertyInvestments": [
            "805432"
        ]
    }
}

Your are lucky enough that also though have two arrays in that response, there is only a single object in each. If there was more than one object, then you cannot guarantee the order of an array and this test would be brittle and fail on occasion.

Final point. If you compare the whole response like this, if\when it fails, then the error message reported in the test tab will probably get cut off and be very hard to work out which element failed.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.