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:58 AM
Thank you for your response, Please provide why we need to use this format. It will helpful to understand.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 05:09 AM
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.
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 05:58 AM
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!