{"record":{"id":"98f6687929002cb9","repo":"Stirling-Tools/Stirling-PDF","slug":"file-is-not-valid-json-err-as-error-message","errorCode":null,"errorMessage":"File is not valid JSON: ${(err as Error).message}","messagePattern":"File is not valid JSON: (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"frontend/editor/src/core/utils/automationConverter.ts","lineNumber":394,"sourceCode":"  if (hasOperations && !hasPipeline) return \"automate\";\n  return \"unknown\";\n}\n\n/**\n * Parse a JSON file's text content into a normalized AutomationConfig.\n * Auto-detects the format unless `expectedFormat` is supplied; throws with a\n * user-readable message on any structural problem.\n */\nexport function parseAutomationFile(\n  fileText: string,\n  toolRegistry: Partial<ToolRegistry>,\n  expectedFormat?: \"automate\" | \"folderScanning\",\n): ParsedAutomationImport {\n  let raw: unknown;\n  try {\n    raw = JSON.parse(fileText);\n  } catch (err) {\n    throw new Error(`File is not valid JSON: ${(err as Error).message}`, {\n      cause: err,\n    });\n  }\n\n  const detected = detectAutomationFormat(raw);\n  const format = expectedFormat ?? detected;\n\n  if (expectedFormat && detected !== \"unknown\" && detected !== expectedFormat) {\n    throw new Error(\n      `Expected ${\n        expectedFormat === \"automate\"\n          ? \"Automate JSON (operations array)\"\n          : \"Folder Scanning JSON (pipeline array)\"\n      } but file looks like ${\n        detected === \"automate\" ? \"Automate JSON\" : \"Folder Scanning JSON\"\n      }.`,\n    );\n  }","sourceCodeStart":376,"sourceCodeEnd":412,"githubUrl":"https://github.com/Stirling-Tools/Stirling-PDF/blob/9ef20dcab80b85041912f045e17a6aea1d08f969/frontend/editor/src/core/utils/automationConverter.ts#L376-L412","documentation":"Thrown by parseAutomationFile when JSON.parse rejects the file text. The original SyntaxError is attached as cause. The file content is not valid JSON.","triggerScenarios":"The file content is not parseable JSON: an HTML error/login page saved as .json, a truncated download, a UTF-8 BOM, binary, or CSV content.","commonSituations":"Downloaded an HTML error or login page instead of the config; user picked a non-JSON file; encoding/BOM issues; a truncated download.","solutions":["Confirm the file is genuinely JSON by opening it in a text editor.","Strip a leading UTF-8 BOM before parsing.","Validate with a JSON linter.","Surface cause.message to the user for the parse position."],"exampleFix":"// before\nraw = JSON.parse(fileText);\n\n// after\nconst clean = fileText.replace(/^\\uFEFF/, '').trim();\nif (!clean.startsWith('{') && !clean.startsWith('[')) {\n  throw new Error('File does not look like JSON (unexpected first character).');\n}\nraw = JSON.parse(clean);","handlingStrategy":"try-catch","validationCode":"const clean = fileText.replace(/^\\uFEFF/, '').trim();\nif (!clean.startsWith('{') && !clean.startsWith('[')) {\n  throw new Error('Selected file is not JSON.');\n}","typeGuard":"function looksLikeJson(text: string): boolean {\n  const t = text.replace(/^\\uFEFF/, '').trim();\n  return t.startsWith('{') || t.startsWith('[');\n}","tryCatchPattern":"try {\n  return parseAutomationFile(text, toolRegistry, expectedFormat);\n} catch (e) {\n  if (e instanceof Error && /not valid JSON/i.test(e.message)) {\n    throw new Error('The selected file is not valid JSON. Please choose an exported automation config.');\n  }\n  throw e;\n}","preventionTips":["Strip a BOM and trim whitespace before JSON.parse.","Reject files whose first non-whitespace character is not { or [ in the import UI.","Differentiate 'not JSON' from 'wrong shape' in user-facing messages."],"tags":["json","parsing","validation","file-import"],"backgroundTag":null,"analyzedSha":"9ef20dcab80b85041912f045e17a6aea1d08f969","analyzedAt":"2026-08-13T22:11:39.827Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}