- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 09:37 AM
Hi All ,
Kindly let me know how to control duplicate records
Script include : test
test2:function(){
var _strXML = '<results probe_time="1633" result_code="0"> <result> <output> [{"number":"12345" ,"status":"A" , "pcstatus":"DRILL" , "op":"654"} , {"number":"5678" ,"status":"P","pcstatus":"DL" , "op":"980"}] </output> </result></results>';
var objXml = new global.XMLDocument(_strXML);
var arrResults = JSON.parse(_objXml.getNodeText('//output').trim());
var obj ={ };
var arr =[ ];
for(var i=0;i<arrResults.length;i++){
obj.number = arrResults[i].number.toString();
obj.status = arrResults[i].status.toString();
arr.push(obj);
}
return JSON.stringify(arr);
},
---------------------------------------------------------------------------------------------------------------------
background script :
gs.info(new test().test2());
output : [{"number":"12345" ,"status":"A" } , {"number":"12345" ,"status":"A" }]
expected output
output : [{"number":"12345" ,"status":"A" } , {"number":"5678" ,"status":"P" }]
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 10:04 AM
Hi @Supriya25 ,
The issue is with the object that you are using- In your script, you were using the same object over and over again, just changing the details (properties) on the same instead of using a new object for each new set of details.
Please try the below code-
var test2 = function() {
var _strXML = '<results probe_time="1633" result_code="0"><result><output>[{"number":"12345","status":"A","pcstatus":"DRILL","op":"654"},{"number":"5678","status":"P","pcstatus":"DL","op":"980"}]</output></result></results>';
var objXml = new global.XMLDocument(_strXML);
var outputText = objXml.getNodeText('//output').trim();
var arrResults = JSON.parse(outputText);
var arr = [];
for (var i = 0; i < arrResults.length; i++) {
var obj = {
number: arrResults[i].number.toString(),
status: arrResults[i].status.toString()
};
arr.push(obj);
}
var jsonString = JSON.stringify(arr);
return jsonString;
};
// Execute function in background script
gs.info(test2());
The solution is to use a new object each time you want to store a new set of details. This way, each object holds unique information.
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 09:48 AM - edited 05-22-2024 09:59 AM
Incorporate the 'ArrayUtil' script include and use the 'unique(arr)' function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 10:07 AM - edited 05-22-2024 10:11 AM
background script :
gs.info(new test().test2());
output : [{"number":"12345" ,"status":"A" } , {"number":"12345" ,"status":"A" }]
var uniqueArr = new ArrayUtil().unique(new test().test2());
var resul = JSON.parse(uniqueArr);
for(var i=0;i<resul.length;i++)
{
gs.info('Account number :"+resul[i].number.toString())
}
I tried below script, but expected results not coming
Error coming : unexpected comma in array literal.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 10:04 AM
Hi @Supriya25 ,
The issue is with the object that you are using- In your script, you were using the same object over and over again, just changing the details (properties) on the same instead of using a new object for each new set of details.
Please try the below code-
var test2 = function() {
var _strXML = '<results probe_time="1633" result_code="0"><result><output>[{"number":"12345","status":"A","pcstatus":"DRILL","op":"654"},{"number":"5678","status":"P","pcstatus":"DL","op":"980"}]</output></result></results>';
var objXml = new global.XMLDocument(_strXML);
var outputText = objXml.getNodeText('//output').trim();
var arrResults = JSON.parse(outputText);
var arr = [];
for (var i = 0; i < arrResults.length; i++) {
var obj = {
number: arrResults[i].number.toString(),
status: arrResults[i].status.toString()
};
arr.push(obj);
}
var jsonString = JSON.stringify(arr);
return jsonString;
};
// Execute function in background script
gs.info(test2());
The solution is to use a new object each time you want to store a new set of details. This way, each object holds unique information.
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 10:27 AM
Perfect it is working now. Thanks a lot
Please help me with one point here
does this below process will not support ?
var obj ={ }; var arr =[ ]; for(var i=0;i<arrResults.length;i++){ obj.number = arrResults[i].number.toString(); obj.status = arrResults[i].status.toString(); arr.push(obj); }