# How to do test assertion on nested json response body

**URL:** <https://community.postman.com/t/how-to-do-test-assertion-on-nested-json-response-body/5827>\
**Category:** Help Hub\
**Tags:** newman-project\
**Created:** [June 9, 2019, 3:28am UTC](https://community.postman.com/t/how-to-do-test-assertion-on-nested-json-response-body/5827 "2019-06-09T03:28:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tohlaymui35](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tohlaymui35](https://community.postman.com/u/tohlaymui35)\
**Post date:** [June 9, 2019, 3:28am UTC](https://community.postman.com/t/how-to-do-test-assertion-on-nested-json-response-body/5827/1 "2019-06-09T03:28:28Z")

</div>

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));",

---

<div class="post-metadata">

**Author:** ![tohlaymui35](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tohlaymui35](https://community.postman.com/u/tohlaymui35)\
**Post date:** [June 10, 2019, 3:25am UTC](https://community.postman.com/t/how-to-do-test-assertion-on-nested-json-response-body/5827/2 "2019-06-10T03:25:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![postman-paul540](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/postman-paul540/32/149_2.png) [@postman-paul540](https://community.postman.com/u/postman-paul540)\
**Post date:** [June 10, 2019, 7:14am UTC](https://community.postman.com/t/how-to-do-test-assertion-on-nested-json-response-body/5827/3 "2019-06-10T07:14:14Z")

</div>

@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)**

```auto
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:**

```auto
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:**

```auto
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:

```auto
{
	"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”:

```auto
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):

```auto
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. 🤷‍♂️

Hope this helps 👍

---

<div class="post-metadata">

**Author:** ![aamir.ahmed](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@aamir.ahmed](https://community.postman.com/u/aamir.ahmed)\
**Post date:** [June 10, 2019, 8:32am UTC](https://community.postman.com/t/how-to-do-test-assertion-on-nested-json-response-body/5827/4 "2019-06-10T08:32:31Z")

</div>

Hi @tohlaymui35,

I would just like to add that the test assertions mentioned by @postman-paul540 can be seen in the following link for your reference: [https://learning.getpostman.com/docs/postman/scripts/postman\_sandbox\_api\_reference](https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference)

---

<div class="post-metadata">

**Author:** ![tohlaymui35](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tohlaymui35](https://community.postman.com/u/tohlaymui35)\
**Post date:** [June 21, 2019, 1:09pm UTC](https://community.postman.com/t/how-to-do-test-assertion-on-nested-json-response-body/5827/5 "2019-06-21T13:09:23Z")

</div>

pm.test(‘Test Supported Questions Screen’, function() {  
let jsonData = pm.response.json();  
if (jsonData)  
delete jsonData[‘conversationId’];  
let validation = tv4.validate(jsonData, responseSchema);  
pm.response.to.have.jsonSchema(responseSchema);  
pm.expect(validation).to.be.true;

})

I deliberately make some changes to the responseSchema, but test assertion still pass

---

<div class="post-metadata">

**Author:** ![tohlaymui35](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tohlaymui35](https://community.postman.com/u/tohlaymui35)\
**Post date:** [June 21, 2019, 1:47pm UTC](https://community.postman.com/t/how-to-do-test-assertion-on-nested-json-response-body/5827/6 "2019-06-21T13:47:05Z")

</div>

looks like there is a issue with tv4 library  
[https://github.com/postmanlabs/postman-app-support/issues/3173](https://github.com/postmanlabs/postman-app-support/issues/3173)

my validation didn’t work

i deliberately put mismatch in the responseSchema, the test assertion can still passed.

let responseSchema = {  
“sendToUISuccess”: false,  
“response”: {  
“conversationId”: “ed4d1129”,  
“target”: “stella.client”,  
“message”: {  
“content”: {  
“key”: “FeedbackSupportQuestions”,  
“type”: “dialog\_modal”,  
“features”: {  
“enableMenu”: false,  
“enableMicrophone”: true,  
“enableNotch”: false  
},  
“attributes”: {  
“accessibilityLabel”: “supported-questions-screen”,  
“background”: {  
“source”: “local”,  
“uri”: “contentBackground”  
},  
“onClose”: {  
“target”: “local”,  
“command”: “dialog\_modal\_close”  
}  
},  
…

…

…  
“destination”: “stella\_client”  
}  
}

}

pm.test(‘Test Supported Questions Screen’, function() {  
let jsonData = pm.response.json();  
if (jsonData)  
delete jsonData[‘conversationId’];  
let validation = tv4.validate(jsonData, responseSchema);  
pm.expect(validation).to.be.true;

})
