Unexpected end of JSON input
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 04:26 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 04:31 AM
hi @Sirri ,
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.
Add Error Handling:
- Wrap the JSON.parse call in a try-catch block to handle any parsing errors gracefully.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 04:49 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 04:40 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 04:44 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader