course --> Integration Hub: Create New Spokes --> Activity 2 --> "Action" (Get Trivia Question)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
In ServiceNow Course "Integration Hub: Create New Spokes". In the 2nd activity. I have created an "Action" (Get Trivia Question) for my Flow, in the first step i have defined a REST API call. Which executes correctly.
in the second step i wanted to parse the response data into JSON.parse(inputs.step_input_response_body) in the script.
But i am getting an error,
Input Variables are set correctly.
Script part:
(function execute(inputs, outputs) {
if (inputs.step_input_status_code == '200') {
gs.debug("Json parsed response by hikmat:" + inputs.step_input_response_body);
var responseObj = JSON.parse(inputs.step_input_response_body);
outputs.step_output_question = responseObj.result.question;
outputs.step_output_category = responseObj.result.category;
outputs.step_output_difficulty = responseObj.result.difficulty;
outputs.step_output_id = responseObj.result.sys_id;
}
})(inputs, outputs);
Here is the Error: <,Detail: Unexpected token: <
Here is the Log message:
Encountered error executing instruction: ActionErrorEvalInstruction{id=18, conditions=[], statusKey=rooto.__action_status__, dontTreatAsErrorKey=rooto.__dont_treat_as_error__}, errorMessage:Error: <,Detail: Unexpected token: <, errorCode:1
Encountered error executing instruction: OpInstruction{id=16, opClass=com.snc.process_flow.operation.script.ScriptOperation, io=ReadOnlyDefaultOutputsIo{input={mid_selection=StringValue{fValue='auto_select'}, step_input_response_body=FlowReference{value=s1o.response_body}, step_input_status_code=FlowReference{value=s1o.status_code}, capabilities=StringValue{fValue=''}, connection_alias=StringValue...
The issue is identified by me. The response body is in HTML format. But according to the learning instruction. It should be a json reponse. Can someone provide a solution?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
The error Unexpected token: < is a sign that your REST API response is returning HTML (which starts with <) instead of JSON. When JSON.parse() hits that < as the first character, it fails immediately.
You've already identified the root cause correctly — the response body is HTML, not JSON. Here are the most common reasons this happens and how to fix them:
1. Missing or incorrect Accept header on your REST step
In your REST API step configuration, make sure you've added a request header with name Accept and value application/json. Without this, many APIs default to returning HTML. Also add Content-Type: application/json if applicable.
2. The API endpoint URL is wrong or redirecting
If the URL is slightly off (missing a path segment, wrong version, etc.), many web servers return an HTML error page or login page instead of a JSON response. Double-check that the full URL matches exactly what the course materials specify. A redirect (301/302) to a login page is a very common culprit — you'll get a 200 status code back, but the body is an HTML login form.
3. Authentication isn't configured properly
If the API requires authentication and your credentials or connection alias aren't set up correctly, the server may respond with an HTML "unauthorized" or login page. Verify your connection alias and credentials are working.
4. Add a defensive check in your script
Regardless of the root cause, it's good practice to validate the response before parsing:
(function execute(inputs, outputs) {
if (inputs.step_input_status_code == '200') {
var body = inputs.step_input_response_body + '';
gs.debug("Response body by hikmat: " + body);
// Check if response is actually JSON before parsing
if (body.trim().indexOf('{') !== 0 && body.trim().indexOf('[') !== 0) {
gs.error("Expected JSON but received non-JSON response: " + body.substring(0, 200));
return;
}
var responseObj = JSON.parse(body);
outputs.step_output_question = responseObj.result.question;
outputs.step_output_category = responseObj.result.category;
outputs.step_output_difficulty = responseObj.result.difficulty;
outputs.step_output_id = responseObj.result.sys_id;
}
})(inputs, outputs);
5. Test the endpoint directly first
Use a REST Message test or the REST API Explorer to call the same endpoint and inspect the raw response. This will confirm whether the issue is with the endpoint itself or with how the action is configured.
In most cases for this particular course exercise, the fix is simply adding the Accept: application/json header to the REST step.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
Hi @HikmatUllaH
It seems its a JSON parsing issue.
Can you try with it .
(function execute(inputs, outputs) {
var parser = new JSON();
var responseObj = parser.decode(inputs.parameter);
if (responseObj.step_input_status_code == '200') {
gs.debug("Json parsed response by hikmat:" + responseObj.step_input_response_body);
outputs.step_output_question = responseObj.result.question;
outputs.step_output_category = responseObj.result.category;
outputs.step_output_difficulty = responseObj.result.difficulty;
outputs.step_output_id = responseObj.result.sys_id;
}
})(inputs, outputs);
