Skip to content

Diagnostic reference

JSON.parse errors in Apps Script

Why JSON.parse fails on empty, incomplete, or non-JSON responses, and what to check before parsing a UrlFetchApp response.

Updated September 12, 2026

Apps Script error

SyntaxError: Unexpected end of JSON input

What it means

Unexpected end of JSON input usually means the body is empty or incomplete. The example below fails when the endpoint returns an empty body. Other parse errors can mean the response contains HTML or malformed JSON. Read an HTTPResponse with getContentText(), then check the status and expected format; extracting text alone does not make it valid JSON.

Related error messages

  • SyntaxError: Unexpected token < in JSON at position 0

Before and after

See the change before you make it.

Problem
function fetchStatus() {
  const response = UrlFetchApp.fetch('https://api.example.com/status');
  const data = JSON.parse(response.getContentText());
  return data.status;
}
Safer version
function fetchStatus() {
  const response = UrlFetchApp.fetch('https://api.example.com/status');
  const text = response.getContentText();
  if (!text.trim()) throw new Error('The endpoint returned an empty body.');
  const data = JSON.parse(text);
  return data.status;
}

How to fix it

  • Read the response body with getContentText().
  • Reject an empty body before calling JSON.parse.
  • Check the HTTP status and response format if parsing still fails.

What stays with you

  • Verify the endpoint returns JSON; an HTML body may come from an authentication, routing, or server error.
  • myFunction does not retry the request.