So iam trying to perform sorting by “icode” field in nested array here is the response that i got in POSTMAN as you can see there are multiple nested array so inside each “dc” array “icode” is displaying in sorted order so in postman i want to write a script to verify weather “icode” inside every “dc” array are in sort order or not can anyone help me with this
{
“data”:[
{
“abc”:[
{
“dc” : “example1”,
“item”:[
{
“iCode”: “-xyz”;
“Name” : 'test1";
},
{
“iCode”: “01”;
“Name” : 'test1";
},
{
“iCode”: “1”;
“Name” : 'test1";
}
]
},
{
“dc” : “example2”,
“item”:[
{
“iCode”: “6”;
“Name” : 'test1";
},
{
“iCode”: “7”;
“Name” : ‘test1’;
},
{
“iCode”: “8”;
“Name” : 'test2";
}
]
},
{
“dc” : “example3”,
“item”:[
{
“iCode”: “x”;
“Name” : 'test1";
},
{
“iCode”: “y”;
“Name” : 'test1";
},
{
“iCode”: “z”;
“Name” : 'test1";
}
]
}
]
}
]
}
I have tried the following code which is not working for me . i don’t know to check the sorting order for every iCode in every dc array
var_= require(‘lodash’);
//parse array return in response body
responseArray =JSON.parse(responseBody);
pm.test(‘iCode are in sorted order’,() => {
var expectedSortedOrder = _.orderBy(responseArray.item,["iCode],[‘asc’]);
pm.expect(responseArray.item).to.eq1(expectedSortedOrder);
Hey @material-specialist1 Welcome to the Postman Community
I have tested with the following script and it seems to return an expected result
const lodash = require('lodash')
const array = {
'abc': [
{'dc': 'example1',
'item': [
{'iCode': 1},
{'iCode': 2},
{'iCode': 3},
]},
{'dc': 'example2',
'item': [
{'iCode': 'a'},
{'iCode': 'b'},
{'iCode': 'c'},
]},
{'dc': 'example3',
'item': [
{'iCode': 'x'},
{'iCode': 'z'},
{'iCode': 'y'},
]}
]
}
pm.test('iCode are in sorted order', () => {
for (var i = 0; i < array.abc.length; i++) {
const itemArray = lodash.map(array.abc[i].item,'iCode')
const orderedItemArray = lodash.orderBy(itemArray)
pm.expect(itemArray).to.eql(orderedItemArray)
console.log(orderedItemArray)
}
})
Hope this helps!
2 Likes
Hi, the above code only printing ‘iCode are in sorted order’ in test result how to print the ordered ItemArray in Test result
- And also how to compare the count of “iCode” and “dc” with the response body count
like for example in response if total count of iCode is 9 and count of dc is 3 so same count i want to verify in my script and test result
also i want to know that “const itemArray = lodash.map(array.abc[i].item,‘iCode’)” is it performing sorting on only first array i.e icode present in ‘dc’: ‘example1’ or is it performing sorting on iCode present in all the “dc” i.e example1, example 2, example3
Hey @material-specialist1!
“const itemArray = lodash.map(array.abc[i].item,‘iCode’)” is it performing sorting on only first array i.e icode present in ‘dc’: ‘example1’ or is it performing sorting on iCode present in all the “dc” i.e example1, example 2, example3
This part is only generating an array from the object. Please see the documentation here:
https://lodash.com/docs/4.17.15#map