Could you please help me on this issue
need to verify the values in the array T2, however the array T2 is dynamic in nature and the location of the array will change dynamically from (0,1), however the below test codes are not working in this case.
Can you help me on this code
“body”: {
"Inventory1": [
{
"id1": 1234,
"id2": "test1",
"W1": [
{
"SKU1": "23423",
"product1": "Test1",
"Transaction1": {
"Product1": "",
"Product1": "23432",
},
"T2": [
{
"SKU1": "67575",
"product1": "Test2",
}
]
},
{
"SKU": "6456",
"product": "Test2",
"Transaction2": {
"Product2": "",
"Product2": "23432"
}
}
],
"T3": [
{
"SKU": "5435",
"product": "Test3",
}
]
}
],
}
let T2 = jsonData.body.Inventory1[0].W1.forEach(a => a.SKU1 === '67575');
pm.expect(T2.SKU1).to.be.eql('67575');
pm.expect(T2.product1).to.be.eql('Test2');
@automationtech7
T2 is a JavaScript object, not a JavaScript array, so you can’t use ForEach.
If you console log the results of T2, you’ll find its undefined.
It also looks like you also are searching for SKU1 at the wrong level.
Don’t you need the SKU1 that is under T2, not the SKU1 that is directly under W1.
To be able to map the data into an array so you can perform an assertion, you need to have something that is static. I can’t see how you can map the data if its dynamic.
For example, is it always called T2 (as that is something we can probably work with).
@michaelderekjones
yes, it’s returning undefined. I tried the below codes and still have the same issue
Can you help me on this code
pm.test(‘Verify the Data’, function() {
let T2 = jsonData.body.Inventory1[0].W1.find(a => a.SKU1 === ‘67575’);
pm.expect(T2.SKU1).to.be.eql(‘67575’);
pm.expect(T2.product1).to.be.eql(‘Test2’);
});
This is still returning undefined, as the element you are searching for is not under W1, but under W1.T2.
As you’ve mentioned that T2 is Dynamic, I’m not sure how this will work.
This seems to work and is based off the following article which has some useful functions for searching objects.
How to parse and search JSON in JavaScript | TechSlides
var jsonData = {"body": {
"Inventory1": [
{
"id1": 1234,
"id2": "test1",
"W1": [
{
"SKU1": "23423",
"product1": "Test1",
"Transaction1": {
"Product1": "",
"Product1": "23432",
},
"T2": [
{
"SKU1": "67575",
"product1": "Test2",
}
]
},
{
"SKU": "6456",
"product": "Test2",
"Transaction2": {
"Product2": "",
"Product2": "23432"
}
}
],
"T3": [
{
"SKU": "5435",
"product": "Test3",
}
]
}
],
}
}
//return an array of objects according to key, value, or key and value matching
function getObjects(obj, key, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjects(obj[i], key, val));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push(obj);
} else if (obj[i] == val && key == ''){
//only add if the object is not already in the array
if (objects.lastIndexOf(obj) == -1){
objects.push(obj);
}
}
}
return objects;
}
// creates array of the search results
var search = getObjects(jsonData,'SKU1','67575');
console.log(search);
pm.test("SKU1 = 67575 and Product1 = Test2", () => {
pm.expect(search[0].SKU1).to.eql('67575');
pm.expect(search[0].product1).to.eql('Test2');
});
Console Log showing search result that contains only the key we are interested in.
Test Results