Error: Unexpected token in object literal

Rajini2
Mega Sage

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"

 

Rajini2_0-1757103238580.png

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.

Rajini2_1-1757103611125.png

 
(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);

 

8 REPLIES 8

Chavan AP
Tera Guru

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();
        
    
Chavan AP
[ Architect | Certified Professional]

Was this response helpful? If so, please mark it as Helpful and Accept as Solution to help others find answers.

Got the same error.

Rajini2_0-1757107544388.png

 

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);
Chavan AP
[ Architect | Certified Professional]

Was this response helpful? If so, please mark it as Helpful and Accept as Solution to help others find answers.

@Rajini2 

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

Chavan AP
[ Architect | Certified Professional]

Was this response helpful? If so, please mark it as Helpful and Accept as Solution to help others find answers.