Error: Unexpected token in object literal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
This is my Action script step, trying to use this in the flow whenever variables (special char escaped) need to be updated. Now this is throwing an error when I am passing this value to variable "cable_installation_request_for_printer_selection_status"
Error:
{"cable_installation_request_for_printer_selection_status":"\"HP Inc DesignJet T1600 36" PostScript 3EK11A#B1K (HP Inc - 3EK11A#B1K) - \" : \"Complete\"","cable_installations_reviewed":"1"}
But, when I test the action separately, it is a success. you can see below how 36\" is escaped.
{"cable_installation_request_for_printer_selection_status":"\"HP Inc DesignJet T1600 36\" PostScript 3EK11A#B1K (HP Inc - 3EK11A#B1K) - \" : \"Complete\""}
Not sure how to fix this script. Need some help here.
(function execute(inputs, outputs) {
var variables = JSON.parse(inputs.variables);
for (var key in variables) {
var valueStr = variables[key].toString();
var escapedValue = valueStr.replace(/"/g, '\\\"'); // Correct: escape double quotes
var finalValue = JSON.stringify(escapedValue).slice(1, -1);
inputs.ritm.variables[key] = finalValue;
}
inputs.ritm.update();
})(inputs, outputs);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Please try below:
(function execute(inputs, outputs) {
var variables = JSON.parse(inputs.variables);
for (var key in variables) {
if (variables.hasOwnProperty(key)) {
var value = variables[key];
// Simply assign the value - ServiceNow handles escaping internally
inputs.ritm.variables[key] = value;
}
}
inputs.ritm.update();
[ Architect | Certified Professional]
Was this response helpful? If so, please mark it as ✅ Helpful and ✅ Accept as Solution to help others find answers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Got the same error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
looks like json is corrupted before it reach - try below once:
(function execute(inputs, outputs) {
try {
var rawVariables = inputs.variables;
// Log the raw input for debugging
gs.info('Raw variables input: ' + rawVariables);
// Sanitize the JSON string before parsing
var sanitizedJson = sanitizeJsonString(rawVariables);
gs.info('Sanitized JSON: ' + sanitizedJson);
var variables = JSON.parse(sanitizedJson);
for (var key in variables) {
if (variables.hasOwnProperty(key)) {
var value = variables[key];
// Clean the value further if needed
if (typeof value === 'string') {
value = cleanValue(value);
}
gs.info('Setting variable ' + key + ' = ' + value);
inputs.ritm.variables[key] = value;
}
}
inputs.ritm.update();
outputs.success = true;
} catch (error) {
gs.error('Script error: ' + error.getMessage());
gs.error('Raw input was: ' + inputs.variables);
outputs.success = false;
outputs.message = error.getMessage();
}
// Function to sanitize malformed JSON
function sanitizeJsonString(jsonStr) {
if (!jsonStr) return '{}';
// Fix common JSON malformation issues
var fixed = jsonStr
// Fix the malformed \" : \" pattern
.replace(/\\" : \\"/g, '": "')
// Fix unescaped quotes in the middle of strings
.replace(/([^\\])"([^"]*[^\\])"([^:])/g, '$1\\"$2\\"$3')
// Fix specific case: 36" should be 36\"
.replace(/36"/g, '36\\"')
// Fix any remaining standalone quotes that aren't escaped
.replace(/([^\\])"/g, '$1\\"')
// Clean up any double escaping that might occur
.replace(/\\\\"/g, '\\"');
return fixed;
}
// Function to clean individual values
function cleanValue(value) {
return value
.replace(/^"/, '') // Remove leading quote
.replace(/"$/, '') // Remove trailing quote
.replace(/\\"/g, '"') // Unescape quotes
.trim();
}
})(inputs, outputs);
[ Architect | Certified Professional]
Was this response helpful? If so, please mark it as ✅ Helpful and ✅ Accept as Solution to help others find answers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Glad I could help! If this solved your issue, please mark it as ✅ Helpful and ✅ Accept as Solution so others can benefit too.
Chavan A.P. Technical Architect | Certified Professional
[ Architect | Certified Professional]
Was this response helpful? If so, please mark it as ✅ Helpful and ✅ Accept as Solution to help others find answers.