Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2024 12:26 PM - edited 08-19-2024 12:27 PM
Hi @Charan123 ,
var text = "Incident : INC001000 Affected User : Abraham Lincoln Priority : 2 - High Assignment Group : Application Developement 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)/);
//gs.info(typeof(incidentsArray))
var ans = JSON.stringify(incidentsArray);
// gs.info(ans);
//gs.info(incidentsArray.length);
var finalArray = [];
for(var i = 0; i < incidentsArray.length; i++){
var text1 = incidentsArray[i];
// Regex to match the incident details
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]+)/g;
var match;
// Loop through each match
if ((match = regex.exec(text1)) !== null) {
// Create an object for each match and push it to the array
finalArray.push({
"Incident": match[1],
"Affected User": match[2].trim(),
"Priority": match[3].trim(),
"Assignment Group": match[4].trim()
});
}
}
gs.info((JSON.stringify(finalArray)));
First the entire data is divided by incidentsArray. Then, for every incidentArray is matched with the regex and pushed into the final array.
Run in the scripts - background to validate the output.
*** Script: [{"Incident":"INC001000","Affected User":"Abraham Lincoln","Priority":"2 - High","Assignment Group":"Application Developement"},{"Incident":"INC001002","Affected User":"Test","Priority":"3 - Moderate","Assignment Group":"Service Desk"}]
If this information helps you, kindly mark this as Helpful and Accept the solution.
Regards,
Najmuddin.