{"record":{"id":"bfe65ff9d73a9fb0","repo":"different-ai/openwork","slug":"json-parse-received-invalid-json-error-instance","errorCode":null,"errorMessage":"JSON.parse received invalid JSON: ${error instanceof Error ? error.message : String(error)}","messagePattern":"JSON\\.parse received invalid JSON: (.+?)","errorType":"exception","errorClass":"InterpreterRuntimeError","httpStatus":null,"severity":"error","filePath":"packages/codemode/src/stdlib/json.ts","lineNumber":34,"sourceCode":"      if (Array.isArray(replacer) || replacer instanceof CodeModeFunction) {\n        throw new InterpreterRuntimeError(\n          \"JSON.stringify replacers are not supported in CodeMode.\",\n          node,\n          \"UnsupportedSyntax\",\n          [supportedSyntaxMessage],\n        )\n      }\n      const space = args[2]\n      const indent = typeof space === \"number\" || typeof space === \"string\" ? space : undefined\n      return JSON.stringify(copyOut(copyIn(args[0], \"JSON.stringify value\")), null, indent)\n    }\n    case \"parse\": {\n      const text = args[0]\n      if (typeof text !== \"string\") throw new InterpreterRuntimeError(\"JSON.parse expects a string.\", node)\n      try {\n        return copyIn(JSON.parse(text), \"JSON.parse result\")\n      } catch (error) {\n        throw new InterpreterRuntimeError(\n          `JSON.parse received invalid JSON: ${error instanceof Error ? error.message : String(error)}`,\n          node,\n        ).as(\"SyntaxError\")\n      }\n    }\n  }\n  throw new InterpreterRuntimeError(`JSON.${name} is not available in CodeMode.`, node)\n}\n","sourceCodeStart":16,"sourceCodeEnd":43,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/packages/codemode/src/stdlib/json.ts#L16-L43","documentation":"When the string passed to JSON.parse is not valid JSON, the sandbox catches the underlying SyntaxError and rethrows it as an InterpreterRuntimeError whose message embeds the parser's own explanation, tagged with .as(\"SyntaxError\") so downstream code can still treat it as a syntax error. The library does this to keep the error inside its interpreter error model while preserving the original diagnostic.","triggerScenarios":"Calling JSON.parse(text) where text contains malformed JSON: trailing commas, single quotes instead of double quotes, unquoted keys, truncated payloads, or empty string.","commonSituations":"Parsing truncated streaming responses; parsing config snippets copy-pasted from JSON5/JS object literals (single quotes, comments); parsing an empty file; server returned an HTML error page instead of JSON.","solutions":["Fix the JSON text: validate it with a linter or JSON.parse in a try block in regular JS to see the exact fault.","Inspect the embedded message after the colon — it names the position of the syntax fault.","If input may be empty or partial, guard with a check like if (!text) before parsing."],"exampleFix":"// before\nconst cfg = JSON.parse(partialChunk)\n// after\nif (!partialChunk.trim()) return null\nconst cfg = JSON.parse(partialChunk)","handlingStrategy":"try-catch","validationCode":"const looksLikeJson = (s: string) => s.trim().length > 0 && /^[\\[\\{]/.test(s.trim()) || /^[\"\\d\\-tfn]/.test(s.trim())","typeGuard":"const isNonEmptyString = (v: unknown): v is string => typeof v === \"string\" && v.trim().length > 0","tryCatchPattern":"try { return JSON.parse(text) } catch (e) { if (e instanceof InterpreterRuntimeError && text.length === 0) return null; throw e }","preventionTips":["Validate JSON payloads with a linter before shipping them into sandbox code","Guard against empty or truncated input before parsing","Strip non-JSON error pages (HTML) before parsing HTTP responses","Use double quotes and no trailing commas when hand-writing JSON"],"tags":["json","codemode","syntax-error","invalid-json"],"backgroundTag":"invalid-json-parse-input","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}