Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to pass description value to objects and then push to array

Charan123
Tera Expert

Hi Team,

convert description value into objects and then push to array.

Charan123_0-1724080929643.png

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

6 REPLIES 6

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

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.