Unexpected end of JSON input

Sirri
Tera Guru

Hi All,

 

We have below requirement If action have five drop downs

Action:1) Add Assitant 

            2) Change Assistant

            3) Remove Assistant

            4)Add Delegate Access (non-assistant)

           5) Remove Delegate Access non assistant

 

I'm trying to achieve this on submit client script using this script but we have selected options in action as Remove Assistant & Remove Delegate Access non assistant if we select these option in action when we submit we are getting the this error('Unexpected end of JSON input' ) in JavaScript  console. please let know the reason why we are getting this error in this two case only please provide the exact solution.

 

var peopleSupported = JSON.parse(g_form.getValue("IO:7b2ca032db36d01074e716f35b9619b3"));
     var removeassistant = g_form.getValue('assistant_name_to_add');
     var action = g_form.getValue('action');
     var previousassistant = g_form.getValue('previous_assistant_name');
     var NewAssitstant = g_form.getValue('new_assistant_name');
     //alert(action);

     for (var i = 0; i < peopleSupported.length; i++) {
         var name = peopleSupported[i].names_of_the_people_supported;
         // Compare variable name to other values
         if ((action == 'Add Assitant' || action == 'Add Delegate Access (non-assistant)') && (removeassistant == name)) {
             // g_form.clearValue('assistant_name_to_add');
             //g_form.showFieldMsg('assistant_name_to_add', 'Assistant or Delegate Name to Add and Names of the people supported cannot be the same.', 'error');
             alert('Assistant or Delegate Name to Add and Names of the people supported cannot be the same.');
             return false; // Prevent form submission
         } else if (action == 'Change Assistant' && previousassistant == NewAssitstant) {
             alert('Previous assitant/New Assitant/People supported cannot be same');
             return false;
         } else if (action == 'Change Assistant' && previousassistant == name) {
             alert('Previous assitant/New Assitant/People supported cannot be same');
             return false;
         } else if (action == 'Change Assistant' && NewAssitstant == name) {
             alert('Previous assitant/New Assitant/People supported cannot be same');
             return false;
         }

     }
     return true;
 
Please let me know what I'm missing here. please provide exact code.
7 REPLIES 7

@Ankur Bawiskar ,

 

Thank you for your response, Please provide why we need to use this format. It will helpful to understand.

@Sirri 

Did it work?

This script includes a try-catch block to handle any JSON parsing errors and ensures that peopleSupported is an array even if the JSON parsing fails. This should help you identify and resolve the issue causing the "Unexpected end of JSON input" error.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

sunil maddheshi
Tera Guru

@Sirri 

The error "Unexpected end of JSON input" occurs when JSON.parse() is given an invalid or empty string. This usually happens when the field "IO:7b2ca032db36d01074e716f35b9619b3" (which stores peopleSupported) is either:

  • Empty ("")

  • Null (null)

  • Not a valid JSON string

 

The field "IO:7b2ca032db36d01074e716f35b9619b3" might be empty or contain invalid JSON only when selecting "Remove Assistant" or "Remove Delegate Access (non-assistant)".

  • These actions might not require a value in peopleSupported, leading to an empty string being passed to JSON.parse(), which causes the error.Modify your script to check if the value is empty or null before parsing:

// Get the value safely
var peopleSupportedValue = g_form.getValue("IO:7b2ca032db36d01074e716f35b9619b3");

// Check if the field is empty or invalid JSON
var peopleSupported = [];
if (peopleSupportedValue) {
    try {
        peopleSupported = JSON.parse(peopleSupportedValue);
    } catch (e) {
        console.error("Invalid JSON in peopleSupported: ", peopleSupportedValue);
        peopleSupported = []; // Set to empty array to prevent errors
    }
}

var removeassistant = g_form.getValue('assistant_name_to_add');
var action = g_form.getValue('action');
var previousassistant = g_form.getValue('previous_assistant_name');
var NewAssitstant = g_form.getValue('new_assistant_name');

// Loop through peopleSupported safely
for (var i = 0; i < peopleSupported.length; i++) {
    var name = peopleSupported[i].names_of_the_people_supported;

    if ((action == 'Add Assitant' || action == 'Add Delegate Access (non-assistant)') && (removeassistant == name)) {
        alert('Assistant or Delegate Name to Add and Names of the people supported cannot be the same.');
        return false;
    } else if (action == 'Change Assistant' && (previousassistant == NewAssitstant || previousassistant == name || NewAssitstant == name)) {
        alert('Previous Assistant/New Assistant/People supported cannot be the same');
        return false;
    }
}

return true;

Please mark correct/helpful if this helps you!