Skip to content

Diagnostic reference

GoogleJsonResponseException: bigquery.jobs.query failed

Fix GoogleJsonResponseException on BigQuery.Jobs.query in Apps Script: set useLegacySql: false for Standard SQL and add Drive scope for external tables.

Updated December 6, 2026

Apps Script error

GoogleJsonResponseException: API call to bigquery.jobs.query failed with error: Encountered " "`" "` "" at line 1...

What it means

The Advanced BigQuery v2 service defaults to Legacy SQL (useLegacySql: true) even though the BigQuery console uses GoogleSQL (Standard SQL). Backtick-quoted table names, WITH CTEs, UNNEST(), and SAFE_CAST() fail until you pass useLegacySql: false. A second common failure is querying a Sheets-backed external table without Drive OAuth scope.

Related error messages

  • GoogleJsonResponseException: API call to bigquery.jobs.query failed with error: Access Denied: BigQuery BigQuery: Permission denied while getting Drive credentials.
  • GoogleJsonResponseException: API call to bigquery.jobs.query failed with error: Syntax error: Unexpected keyword WITH

Before and after

The code before and after the fix.

Problem 1 changed line
function queryRecentOrders(projectId) {
  // BigQuery API v2 defaults to useLegacySql: true, which rejects Standard SQL backticks and CTEs.
  return BigQuery.Jobs.query(
    {
      query: 'SELECT id, total FROM `my-project.sales.orders` LIMIT 50',
    },
    projectId,
  );
}
Fixed 1 changed line
function queryRecentOrders(projectId) {
  return BigQuery.Jobs.query(
    {
      query: 'SELECT id, total FROM `my-project.sales.orders` LIMIT 50',
      useLegacySql: false,
    },
    projectId,
  );
}

How to fix it

  • Add useLegacySql: false to the BigQuery.Jobs.query() request object whenever your query uses GoogleSQL (Standard SQL).
  • If the error says Permission denied while getting Drive credentials, add https://www.googleapis.com/auth/drive.readonly to oauthScopes in appsscript.json and re-authorize.
  • Check that the executing account has BigQuery Job User and Data Viewer IAM roles on the target project and dataset.

Things to check

  • BigQuery.Jobs.query() returns up to 10 MB per page and defaults to a 10-second timeout; loop with BigQuery.Jobs.getQueryResults() and pageToken for larger result sets.
  • myFunction catches Standard SQL queries missing useLegacySql: false statically under rule MF95031.

In the Apps Script editor

Catch errors like this before clicking Run.

myFunction (published as Error Checker for Google Apps Script on the Chrome Web Store) checks your code while you edit at script.google.com. Diagnostics, autocomplete, and rule-based quick fixes are free without an account, run in your browser, and request no Google Drive, Gmail, or Apps Script project OAuth scopes. Writing Apps Script with an AI coding agent? Connect the Apps Script MCP server to run the same static checks before pasting code.