angular/angular-cli · error · Error

Failed to parse "${this.path}" as JSON AST Object. ${printPa

Error message

Failed to parse "${this.path}" as JSON AST Object. ${printParseErrorCode(error)} at location: ${offset}.

What it means

The JsonAst helper parses a JSON file (angular.json/workspace config) into an AST with the JSON parser. When the file content contains a syntax error, the parse records errors and the class throws with the error code and byte offset of the first problem.

Source

Thrown at packages/schematics/angular/utility/json-file.ts:49

  constructor(
    private readonly host: Tree,
    private readonly path: string,
  ) {
    this.content = this.host.readText(this.path);
    this.eol = getEOL(this.content);
  }

  private _jsonAst: Node | undefined;
  private get JsonAst(): Node | undefined {
    if (this._jsonAst) {
      return this._jsonAst;
    }

    const errors: ParseError[] = [];
    this._jsonAst = parseTree(this.content, errors, { allowTrailingComma: true });
    if (errors.length) {
      const { error, offset } = errors[0];
      throw new Error(
        `Failed to parse "${this.path}" as JSON AST Object. ${printParseErrorCode(
          error,
        )} at location: ${offset}.`,
      );
    }

    return this._jsonAst;
  }

  get(jsonPath: JSONPath): unknown {
    const jsonAstNode = this.JsonAst;
    if (!jsonAstNode) {
      return undefined;
    }

    if (jsonPath.length === 0) {
      return getNodeValue(jsonAstNode);
    }

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Open the file at the reported offset, fix the JSON syntax error, and re-run the command.
  2. Validate the file with a JSON linter or `node -e "JSON.parse(require('fs').readFileSync('angular.json'))"`.
  3. Restore angular.json from git (`git checkout -- angular.json`) if the file was corrupted by tooling.

Example fix

// before (angular.json)
{ "projects": { "app": { ... ,, } }
// after
{ "projects": { "app": { ... } } }
Defensive patterns

Strategy: try-catch

Validate before calling

try { JSON.parse(fs.readFileSync('angular.json', 'utf8')); } catch (e) { /* fix before running schematics */ }

Try / catch

try {
  await schematicRunner.run(...);
} catch (e) {
  if (/Failed to parse .* as JSON/.test(e.message)) {
    const m = e.message.match(/at location: (\d+)/);
    console.error('Invalid JSON at offset', m && m[1]);
  } else throw e;
}

Prevention

When it happens

Trigger: Any schematic or builder reading a JSON file (typically angular.json) whose content fails JSON parsing — trailing content, unquoted keys, stray commas, comments, or truncated edits by earlier tools.

Common situations: Manual edits to angular.json introducing invalid JSON (comments, missing quotes); merge conflicts left markers like <<<<<<<; scripts rewriting the config and truncating it; non-UTF8 or BOM issues.

Understand the failure class

Related errors


AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30). Data as JSON: /api/errors/a08fbfaa597575dc. Report an issue: GitHub.