How to pass description value to objects and then push to array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2024 08:26 AM
Hi Team,
convert description value into objects and then push to array.
In the format of [ {Incident : INC0010014, Affected User : Abraham Lincoln, Priority : 2 - High, Assignment Group : Application Development},{ Incident : INC0010020, Affected User : David Miller, Priority : 1 - Critical, Assignment Group : Service Desk}, {....}]
Please share your inputs how to achieve this.
Thanks
Charan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-27-2024 06:00 AM
Hi @Najmuddin Mohd ,
Not working whenever field is missing suppose assignment group is unavailable in first set.
var text = "Incident : INC001000 Affected User : Abraham Lincoln Priority : 2 - High
Incident : INC001002 Affected User : Test Priority : 3 - Moderate Assignment Group : Service Desk"
Could you please suggest on this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-27-2024 11:05 PM
Hi @Charan123 ,
I have updated the regex to include the Affected user, Priority, and Assignment group as optional.
// var text = "Incident : INC001000 Affected User : Abraham Lincoln Priority : 2 - High Incident : INC001002 Affected User : Test Priority : 3 - Moderate Assignment Group : Service Desk "; // string variable
// // Split the text at each "Incident" keyword
// var incidentsArray = text.trim().split(/(?=Incident\s*:)/);
// var finalArray = [];
// for (var i = 0; i < incidentsArray.length; i++) {
// var text1 = incidentsArray[i];
// // Regex to match the incident details, with Assignment Group being optional
// var regex = /Incident\s*:\s*(INC\d+)\s*Affected User\s*:\s*([A-Za-z\s]+)\s*Priority\s*:\s*([^\n]*?)(?:\s*Assignment Group\s*:\s*([A-Za-z\s]+))?\s*$/;
// var match = regex.exec(text1);
// // If a match is found, push the object to the array
// if (match) {
// finalArray.push({
// "Incident": match[1],
// "Affected User": match[2].trim() ? match[2].trim(): "",
// "Priority": match[3].trim(),
// "Assignment Group": match[4] ? match[4].trim() : ""
// });
// }
// }
// gs.info(JSON.stringify(finalArray));
var text = "Incident : INC001000 Affected User : Abraham Lincoln Incident : INC001002 Priority : 3 - Moderate Assignment Group : Service Desk "; // string variable
// Split the text at each "Incident" keyword
var incidentsArray = text.trim().split(/(?=Incident\s*:)/);
var finalArray = [];
for (var i = 0; i < incidentsArray.length; i++) {
var text1 = incidentsArray[i];
// Regex to match the incident details, with Affected User, Priority, and Assignment Group being optional
var regex = /Incident\s*:\s*(INC\d+)(?:\s*Affected User\s*:\s*([A-Za-z\s]+))?(?:\s*Priority\s*:\s*([^\n]*?))?(?:\s*Assignment Group\s*:\s*([A-Za-z\s]+))?\s*$/;
var match = regex.exec(text1);
// If a match is found, push the object to the array
if (match) {
finalArray.push({
"Incident": match[1],
"Affected User": match[2] ? match[2].trim() : "",
"Priority": match[3] ? match[3].trim() : "",
"Assignment Group": match[4] ? match[4].trim() : ""
});
}
}
gs.info(JSON.stringify(finalArray));
Output:
*** Script: [{"Incident":"INC001000","Affected User":"Abraham Lincoln","Priority":"","Assignment Group":""},{"Incident":"INC001002","Affected User":"","Priority":"3 - Moderate","Assignment Group":"Service Desk"}]
The ternary operator in the regex (?:) is used to check if a match exists.
If a field is missing, it is set to "".
If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.