JSON Nested Object - Validate for matching values

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
]
},
“text”: “ignored”,
“identifier”: [
{
“type”: {
“encoding”: [
{
“system”: “http://urlme.com”,
“code”: “ABC”,
“display”: “Display 00”
}
]
},
“value”: “123”
},
{
“type”: {
“encoding”: [
{
“system”: “http://yahoo.com”,
“code”: “ABCD”,
“display”: “Display 01”
}
]
},
“value”: “1777755555”
},
{
“type”: {
“encoding”: [
{
“system”: “https://google.com”,
“code”: “ABCDE”,
“display”: “Display 02”
}
]
},
“value”: “123OLD”
},
{
“type”: {
“encoding”: [
{
“system”: “http://myURL.com”,
“code”: “PER”,
“display”: “Display 03”
}
]
},
“value”: “1111111111”
},
{
“type”: {
“encoding”: [
{
“system”: “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.

Hi @vivekRESTs

You could try something like this;

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…

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.