{"record":{"id":"927973a50065ce0e","repo":"angular/angular-cli","slug":"failed-to-parse-path-as-json-ast-object-pr","errorCode":null,"errorMessage":"Failed to parse \"${path}\" as JSON AST Object. ${printParseErrorCode(error)} at location: ${offset}.","messagePattern":"Failed to parse \"(.+?)\" as JSON AST Object\\. (.+?) at location: (.+?)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/angular/cli/src/utilities/json-file.ts","lineNumber":213,"sourceCode":" */\nexport function readAndParseJson<T extends JsonValue>(path: string): T {\n  const errors: ParseError[] = [];\n  const content = parse(readFileSync(path, 'utf-8'), errors, { allowTrailingComma: true }) as T;\n  if (errors.length) {\n    formatError(path, errors);\n  }\n\n  return content;\n}\n\n/**\n * Formats a JSON parsing error and throws an exception.\n * @param path The path to the file that failed to parse.\n * @param errors The list of parsing errors.\n */\nfunction formatError(path: string, errors: ParseError[]): never {\n  const { error, offset } = errors[0];\n  throw new Error(\n    `Failed to parse \"${path}\" as JSON AST Object. ${printParseErrorCode(\n      error,\n    )} at location: ${offset}.`,\n  );\n}\n\n/**\n * Parses a JSON string, supporting comments and trailing commas.\n * @param content The JSON string to parse.\n * @returns The parsed JSON object.\n */\nexport function parseJson<T extends JsonValue>(content: string): T {\n  return parse(content, undefined, { allowTrailingComma: true }) as T;\n}\n","sourceCodeStart":195,"sourceCodeEnd":228,"githubUrl":"https://github.com/angular/angular-cli/blob/bb72145f9ab45aee29f523236b3a25cd0813a841/packages/angular/cli/src/utilities/json-file.ts#L195-L228","documentation":"`formatError` in the CLI's JSON-file utility converts the first `ParseError` reported by the `jsonc-parser` tokenizer into a descriptive `Error`, including the file path, the parser error code text (`printParseErrorCode`), and the byte offset where parsing failed. It is thrown whenever a JSON file the CLI reads (workspace config, etc.) is not syntactically valid JSON.","triggerScenarios":"Reading a JSON file through the CLI's JSON utilities (e.g. `readAndParseJson`, `JsonAst`) when `jsonc-parser`'s `parse`/`parseTree` reports errors — invalid syntax, unexpected characters, truncated file.","commonSituations":"Hand-edited `angular.json` with typos or trailing commas; merge-conflict markers (`<<<<<<<`) left in the config; a tool writing partial/truncated JSON; HTML error pages saved where a JSON file was expected.","solutions":["Open the file and fix the syntax at the reported offset (the message gives the location).","Run a validator: `jq . <file>` or `npx jsonlint <file>` to pinpoint the problem.","Restore from version control: `git checkout -- angular.json`.","If you edit configs programmatically, validate JSON before saving (e.g. `JSON.parse` round-trip in a pre-commit hook)."],"exampleFix":"// before\n{ \"version\": 1, \"projects\": { } // missing closing brace\n// after\n{ \"version\": 1, \"projects\": { } }","handlingStrategy":"validation","validationCode":"import { readFile } from 'node:fs/promises';\nconst text = await readFile(path, 'utf8');\ntry { JSON.parse(text); } catch (e) {\n  throw new Error(`${path} is not valid JSON; fix syntax before the CLI reads it.`, { cause: e });\n}","typeGuard":"function parsesAsJson(text: string): boolean {\n  try { JSON.parse(text); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  await runNgCommand();\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Failed to parse') && e.message.includes('as JSON AST Object')) {\n    // message contains file, error code, and offset — fix the syntax there\n  } else { throw e; }\n}","preventionTips":["Add a pre-commit hook validating JSON configs (prettier --check or jsonlint).","Search files for leftover merge-conflict markers (<<<<<<<) before running the CLI.","Avoid truncating writes: write configs atomically (temp file + rename).","Use the offset and error code in the message to locate the exact syntax problem."],"tags":["json","parse-error","config","angular-cli"],"backgroundTag":"json-parse-error","analyzedSha":"bb72145f9ab45aee29f523236b3a25cd0813a841","analyzedAt":"2026-08-30T02:47:34.745Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}