Payload Value Genration

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2024 06:52 AM
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 :-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2024 01:32 AM
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 :
If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
Thanks,
BK