{"record":{"id":"3b865f25c01bfba5","repo":"moodle/moodle","slug":"json-parse","errorCode":null,"errorMessage":"JSON.parse","messagePattern":"JSON\\.parse","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"public/lib/yuilib/3.18.1/json-parse-shim/json-parse-shim-debug.js","lineNumber":175,"sourceCode":"        s += '';\n    }\n\n    // Replace certain Unicode characters that are otherwise handled\n    // incorrectly by some browser implementations.\n    // NOTE: This modifies the input if such characters are found!\n    s = s.replace(_UNICODE_EXCEPTIONS, _escapeException);\n\n    // Test for any remaining invalid characters\n    if (!_UNSAFE.test(s.replace(_ESCAPES,'@').\n                        replace(_VALUES,']').\n                        replace(_BRACKETS,''))) {\n\n        // Eval the text into a JavaScript data structure, apply any\n        // reviver function, and return\n        return _revive(eval('(' + s + ')'), reviver);\n    }\n\n    throw new SyntaxError('JSON.parse');\n};\n\n// Property available for testing if the implementation being used\n// is native or a shim\nY.JSON.parse.isShim = true;\n\n\n}, '3.18.1', {\"requires\": [\"json-parse\"]});\n","sourceCodeStart":157,"sourceCodeEnd":184,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/yuilib/3.18.1/json-parse-shim/json-parse-shim-debug.js#L157-L184","documentation":"Y.JSON.parse (the YUI JSON shim) validates text with its internal safety regex before evaluating. When the text fails validation (or the native path is unavailable and the text is not a valid JSON structure), it throws new SyntaxError('JSON.parse'), mirroring native JSON.parse's generic message. The real problem is almost always invalid or non-conforming JSON text, or an unexpected non-string input reaching the parser.","triggerScenarios":"Calling Y.JSON.parse(text) where text contains non-JSON syntax: single quotes, trailing commas, unquoted or single-quoted keys, comments, unescaped control characters, undefined/NaN literals, or empty string; also when eval fallback encounters a parse error.","commonSituations":"Parsing server responses that are actually HTML error pages or empty bodies; hand-built JSON strings with JS-object-literal syntax; parsing 'undefined'/'null string mixups; older browsers lacking native JSON where the shim's stricter safety regex rejects texts native parsers might accept.","solutions":["Log and inspect the exact text before parsing; fix the producer to emit valid JSON (double-quoted keys, no trailing commas/comments)","Wrap Y.JSON.parse in try/catch and fall back to a safe default or surface a clear message","Ensure the endpoint returns Content-Type application/json and status 200 before parsing","If input may be a JS object already, parse only when typeof input === 'string'"],"exampleFix":"// before\nvar data = Y.JSON.parse(xhr.responseText);\n// after\nvar data;\ntry {\n  data = Y.JSON.parse(xhr.responseText);\n} catch (e) {\n  console.error('Invalid JSON:', xhr.responseText.slice(0, 200));\n  data = { error: 'invalid-json' };\n}","handlingStrategy":"validation","validationCode":"function safeJsonParse(text, reviver) {\n  if (typeof text !== 'string' || !/^\\s*[\\[\\{\"]/.test(text)) {\n    throw new SyntaxError('safeJsonParse: input is not JSON text');\n  }\n  return Y.JSON.parse(text, reviver);\n}","typeGuard":"function isJsonString(s) { return typeof s === 'string' && /^(\\s*[\\[{])|(\\s*\"(\\\\.|[^\"\\\\])*\")|\\s*-?\\d|\\s*(true|false|null)\\s*$/.test(s); }","tryCatchPattern":"try {\n  var data = Y.JSON.parse(text);\n} catch (e) {\n  if (e instanceof SyntaxError) {\n    console.error('Invalid JSON received:', String(text).slice(0, 200));\n    data = null;\n  }\n}","preventionTips":["Check xhr.status === 200 and Content-Type application/json before parsing","Log a snippet of the raw text on failure to spot HTML error pages or empty bodies","Generate JSON with JSON.stringify server-side instead of hand-built strings","Use native JSON.parse where available; keep YUI shim only for legacy browsers"],"tags":["json","parse","yui","syntax-error"],"backgroundTag":"json-parse-error","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}