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

Arun_Manoj
Mega Sage

hi @Sirri ,

 

  1. Check the Value of peopleSupported:

    • Ensure that g_form.getValue("IO:7b2ca032db36d01074e716f35b9619b3") returns a valid JSON string. If it returns an empty string or invalid JSON, JSON.parse will throw an error.
  2. Add Error Handling:

    • Wrap the JSON.parse call in a try-catch block to handle any parsing errors gracefully.
  3. Validate Input Before Parsing:

    • Check if the value is not empty or null before attempting to parse it.

Here's an updated version of your script with these improvements:

try {
    var peopleSupportedValue = g_form.getValue("IO:7b2ca032db36d01074e716f35b9619b3");
    if (!peopleSupportedValue) {
        throw new Error("Invalid JSON input");
    }
    var peopleSupported = JSON.parse(peopleSupportedValue);
} catch (e) {
    console.error("Error parsing JSON: ", e.message);
    alert("There was an error processing your request. Please try again.");
    return false; // Prevent form submission
}

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

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; // Prevent form submission
    } else if (action == 'Change Assistant' && previousassistant == NewAssitstant) {
        alert('Previous assistant/New Assistant/People supported cannot be same');
        return false;
    } else if (action == 'Change Assistant' && previousassistant == name) {
        alert('Previous assistant/New Assistant/People supported cannot be same');
        return false;
    } else if (action == 'Change Assistant' && NewAssitstant == name) {
        alert('Previous assistant/New Assistant/People supported cannot be same');
        return false;
    }
}
return true;

This script includes error handling for the JSON parsing and checks if the input is valid before attempting to parse it. This should help prevent the "Unexpected end of JSON input" error.

@Arun_Manoj ,

 

Thank you for your response. when I tried with this script I'm getting this error 'There was an error processing your request. Please try again'. please let me know the reason

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Sirri 

please share how the form looks?

what is the variable types? share screenshots.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Sirri 

try this

try {
    var peopleSupportedValue = g_form.getValue("IO:7b2ca032db36d01074e716f35b9619b3");
    var peopleSupported = peopleSupportedValue ? JSON.parse(peopleSupportedValue) : [];
} catch (e) {
    console.error('Error parsing JSON:', e);
    alert('There was an error processing the form. Please try again.');
    return false;
}

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

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) {
        alert('Previous assistant/New Assistant/People supported cannot be the same');
        return false;
    } else if (action == 'Change Assistant' && previousassistant == name) {
        alert('Previous assistant/New Assistant/People supported cannot be the same');
        return false;
    } else if (action == 'Change Assistant' && NewAssitstant == name) {
        alert('Previous assistant/New Assistant/People supported cannot be the same');
        return false;
    }
}

return true;

If my response helped please mark it correct and close the thread so that it benefits future readers.

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