Payload Value Genration

Community Alums
Not applicable

Hello Team,

 

We have payload in below format :-

 

"fullDescription": "{\"cmdb_ci.impacted\":\"024GPB-UK0ESXI17, 024GPB-UK0MOH03, 024GPB-UK0SUB05\",\"cmdb_ci.environment\":\"\"024GPB-UK0ESXI17-Production\", \"024GPB-UK0MOH03-Production\", \"024GPB-UK0SUB05-Production\"\",\"cmdb_ci.requested_action_SMM\":\"Schedule MM mode\",\"cmdb_ci.schedule_start_date_SMM\":\"2024-10-16 15:07:22\",\"cmdb_ci.schedule_end_date_SMM\":\"2024-10-17 15:07:45\"}",

 

Now I have requirement , \"cmdb_ci.environment\":\"\"024GPB-UK0ESXI17-Production\", \"024GPB-UK0MOH03-Production\", \"024GPB-UK0SUB05-Production\"\", I have to remove those slash and comms and separated like below 

\"cmdb_ci.environment\":\"024GPB-UK0ESXI17, 024GPB-UK0MOH03, 024GPB-UK0SUB05\",

 

for this i have written code to replaced values  :-

var env= current.variables.environment_SMM.getDisplayValue();
var env_ssm=env.replace(/\n/g,",");
 
Could you please what  make change in above code will give me result like below :-
\"cmdb_ci.environment\":\"024GPB-UK0ESXI17, 024GPB-UK0MOH03, 024GPB-UK0SUB05\",
 
Please help . 
 
 

 

1 REPLY 1

Bhavya11
Kilo Patron

hi @Community Alums ,

 

i have try below script in background script please make changes according to you check if this works for you.

 

var jsonString = "{\"cmdb_ci.impacted\":\"024GPB-UK0ESXI17, 024GPB-UK0MOH03, 024GPB-UK0SUB05\",\"cmdb_ci.environment\":\"024GPB-UK0ESXI17-Production, 024GPB-UK0MOH03-Production, 024GPB-UK0SUB05-Production\",\"cmdb_ci.requested_action_SMM\":\"Schedule MM mode\",\"cmdb_ci.schedule_start_date_SMM\":\"2024-10-16 15:07:22\",\"cmdb_ci.schedule_end_date_SMM\":\"2024-10-17 15:07:45\"}";
var parsedJson;
try {
    parsedJson = JSON.parse(jsonString);
} catch (e) {
    gs.error("JSON Parsing Error: " + e);
}

if (parsedJson && parsedJson["cmdb_ci.environment"]) {
    var envString = parsedJson["cmdb_ci.environment"];
    
    var cleanedEnv = envString.replace(/\\\"/g, '') // Remove escaped quotes
                              .replace(/-Production/g, '') // Remove the "-Production" suffix
                              .split(',') // Split by comma
                              .map(function(item) { return item.trim(); }) // Trim each item
                              .filter(function(item) { return item.length > 0; }) // Remove empty items
                              .join(', '); // Join with commas
    
    var formattedEnv = '"cmdb_ci.environment\\":\\"' + cleanedEnv + '\\"",';
    
    gs.info("Formatted Output: " + formattedEnv);
} else {
    gs.info("The 'cmdb_ci.environment' property does not exist in the JSON.");
}

 

 

output :

Bhavya11_0-1729153863013.png

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks,

BK