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 the HTTPResponse with getContentText(), then check the status and the expected format.

Related error messages

  • SyntaxError: Unexpected token < in JSON at position 0

Before and after

The code before and after the fix.

Problem 1 changed line
function fetchStatus() {
  const response = UrlFetchApp.fetch('https://api.example.com/status');
  const data = JSON.parse(response.getContentText());
  return data.status;
}
Fixed 3 changed lines
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.

Things to check

  • Verify the endpoint returns JSON. An HTML body may come from an authentication, routing, or server error.