How to do test assertion on nested json response body

how can I compare the whole json response body with out inspecting every element by element?

like this: pm.expect(JSON.stringify(jsonData[1].message.content)).to.equal(JSON.stringify(data[0].message.content));",
" pm.expect(JSON.stringify(jsonData[3].message.content.content[0].content[0].content[1].content)).to.equal(JSON.stringify(data[2].message.content.content[0].content[0].content[1].content));",
" pm.expect(JSON.stringify(jsonData[3].message.content.content[0].content[1].content[0].content[0].content)).to.equal(JSON.stringify(data[2].message.content.content[0].content[1].content[0].content[0].content));",

1 Like

I found a tv4 library that simplify this test assertion on the json response

1 Like

@tohlaymui35
There are many ways to do what you want, here are 2 ways that I do what you’re asking:

1) Create a JSON schema, save it as a variable (you could save this as an environment or collection variable) and then test that the response body matches the JSON schema:

Currently I have one request where all my JSON schemas are defined (I’ve been meaning to move this as collection variables but I haven’t gotten around to doing this yet)

const schemaNextAction = {
    "type": "object",
    "title": "Schema - Next Action",
    "additionalProperties": false,
    "required": [
        "next_action_id",
        "next_action_by",
        "customer_return_id"
    ],
    "properties": {
        "next_action_id": {
            "type": "integer",
            "minimum": 1
        },
        "next_action_by": {
            "type": "string",
            "minLength": 1
        },
        "customer_return_id": {
            "type": "integer",
            "minimum": 1
        }
    }
};
pm.environment.set("schemaNextAction", schemaNextAction);

In my “Next Action” request, where I actually want to call and use the schema:

const schemaNextAction = pm.environment.get("schemaNextAction");
pm.test("Expects response to match expected JSON Schema", function() {
    pm.response.to.have.jsonSchema(schemaNextAction);
});

2. Use “.to.have.property” in your assertions:

const body = pm.response.json();
pm.test("Expects the customer ID to be 12", function() {
    pm.expect(body).to.have.property("customerID", 12);
});

The great thing about the above test is that it checks several things:

  • It checks that our response body has the key “customerID”, if it doesn’t we get a nice failure message stating this
  • It checks that the value for the “customerID” (assuming that the key exists), is 12. Again, if it’s not - we get a nice failure message.
  • It also does a check on type, so if we get “12” and not the int value 12, it fails and we get a nice failure message.

“.to.have.propery” is my go-to assertion for most things in Postman.

3. You could use the above assertion inside a “forEach” to test several things (I personally don’t do this anymore but I thought I’d show anyway)

Let’s say you have the below response body you want to test against:

{
	"users": [
	{
		"species": "human",
		"name": "Paul",
		"age": 30
	},
	{
		"species": "human",
		"name": "David",
		"age": 32
	},
	{
		"species": "human",
		"name": "Lisa",
		"age": 29
	},
	{
		"species": "human",
		"name": "Craig",
		"age": 26
	}
	]
}

Let’s write a test to assert that every “user” is a “human”:

const body = pm.response.json();
pm.test("Expects every user to be 'human'", function() {
    body.users.forEach(function(user) {
        pm.expect(user).to.have.property("species", "human");
    });
});

Like I said before, I’m not a massive fan of the above example as the failure message isn’t great - each user is also not shown in the test results, unless you write it this way (again, I don’t like doing this either):

const body = pm.response.json();
body.users.forEach(function(user) {
    pm.test("Expects every user to be 'human'", function() {
        pm.expect(user).to.have.property("species", "human");
    });
});

The above will show a “Expects every user to be ‘human’” pass/fail state for every user in the test results, but this can look messy. :man_shrugging:

Hope this helps :+1:

1 Like

Hi @tohlaymui35,

I would just like to add that the test assertions mentioned by @pfarrell can be seen in the following link for your reference: https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference

1 Like