# JSON Nested Object - Validate for matching values

**URL:** <https://community.postman.com/t/json-nested-object-validate-for-matching-values/22504>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [April 12, 2021, 5:45am UTC](https://community.postman.com/t/json-nested-object-validate-for-matching-values/22504 "2021-04-12T05:45:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vivekRESTs](https://avatars.discourse-cdn.com/v4/letter/v/c6cbf5/32.png) [@vivekRESTs](https://community.postman.com/u/vivekRESTs)\
**Post date:** [April 12, 2021, 5:45am UTC](https://community.postman.com/t/json-nested-object-validate-for-matching-values/22504/1 "2021-04-12T05:45:13Z")

</div>

Hi PM Community,  
I am able to access the below mentioned JSON Object’s values using the dot notation but the problem is that the order of the response keeps changing. I am new to implementing lodash methods but I had success with returning them to console.

{  
“resourceType”: “picture”,  
“id”: “VIVEK”,  
“meta”: {  
“versionId”: “v0.3”,  
“lastUpdated”: “timestamp here”,  
“source”: “MyPC”,  
“profile”: [  
“[http://profilereferenceURLhere.com](http://profilereferenceURLhere.com)”  
]  
},  
“text”: “ignored”,  
“identifier”: [  
{  
“type”: {  
“encoding”: [  
{  
“system”: “[http://urlme.com](http://urlme.com)”,  
“code”: “ABC”,  
“display”: “Display 00”  
}  
]  
},  
“value”: “123”  
},  
{  
“type”: {  
“encoding”: [  
{  
“system”: “[http://yahoo.com](http://yahoo.com)”,  
“code”: “ABCD”,  
“display”: “Display 01”  
}  
]  
},  
“value”: “1777755555”  
},  
{  
“type”: {  
“encoding”: [  
{  
“system”: “[https://google.com](https://google.com)”,  
“code”: “ABCDE”,  
“display”: “Display 02”  
}  
]  
},  
“value”: “123OLD”  
},  
{  
“type”: {  
“encoding”: [  
{  
“system”: “[http://myURL.com](http://myURL.com)”,  
“code”: “PER”,  
“display”: “Display 03”  
}  
]  
},  
“value”: “1111111111”  
},  
{  
“type”: {  
“encoding”: [  
{  
“system”: “[http://dummyurl.com](http://dummyurl.com)”,  
“code”: “HUT”,  
“display”: “Display 04”  
}  
]  
},  
“value”: “999777888”  
}  
]  
}

Below is my code to return the code for the first element in the array:  
pm.test(“Test Lodash”, () =\> {

```
    _.each(jd1.json.identifier[0].type.encoding, (item) => {

               console.log(item.code);    

    })

});

```

I wish to validate if the code - “ABC” and the value is “123”. I am having trouble with asserting these values. I tried the below code as well:

pm.test(“Test Lodash 2”, () =\> {  
\_.each(jd1.json.identifier, (item) =\> {  
console.log(item);  
})  
});

This returns a JSON response but I have issues in accessing the values I need to validate them.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [March 15, 2022, 1:33pm UTC](https://community.postman.com/t/json-nested-object-validate-for-matching-values/22504/2 "2022-03-15T13:33:29Z")

</div>

Hi @vivekRESTs

You could try something like this;

```auto
var response = pm.response.json();

for (let i = 0; i < response.identifier.length; i++) {
    console.log(response.identifier[i].type.encoding[0].code);
    console.log(response.identifier[i].value);

    const hasItemDetails = (response.identifier[i].type.encoding[0].code === "ABC" 
    && response.identifier[i].value === "123");

    //Test if Bool is True or False
    pm.test("Is ABC and 123?", function () {
        pm.expect(hasItemDetails).to.be.true;
    });

}

```

Hope it helps…

---

<div class="post-metadata">

**Author:** ![jalbert48](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@jalbert48](https://community.postman.com/u/jalbert48)\
**Post date:** [March 16, 2022, 12:31pm UTC](https://community.postman.com/t/json-nested-object-validate-for-matching-values/22504/3 "2022-03-16T12:31:59Z")

</div>

Assuming you have both objects deserialized, you could easily flatten the two lists containing the inner most `home` objects like so:

List responseHomes = response  
.getWorkGroups()  
.stream()  
.map(WorkgroupExample::getHome)  
.flatMap(List::stream)  
.collect(Collectors.toList());

List requestHomes = payload  
.getWorkGroups()  
.stream()  
.map(WorkgroupExample::getHome)  
.flatMap(List::stream)  
.collect(Collectors.toList());

That way you should end up with two distinct lists, one containing the `home` entries of the `request` object and one containing the `home` entries of the `response` object.
