{"record":{"id":"f00acf7a343cbdbc","repo":"karatelabs/karate","slug":"unexpected-token-c-in-json","errorCode":null,"errorMessage":"Unexpected token '${c}' in JSON","messagePattern":"Unexpected token '(.+?)' in JSON","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsonParser.java","lineNumber":99,"sourceCode":"            }\n            char c = s.charAt(pos);\n            switch (c) {\n                case '{':\n                    return parseObject();\n                case '[':\n                    return parseArray();\n                case '\"':\n                    return parseString();\n                case 't':\n                case 'f':\n                    return parseBoolean();\n                case 'n':\n                    return parseNull();\n                default:\n                    if (c == '-' || (c >= '0' && c <= '9')) {\n                        return parseNumber();\n                    }\n                    throw syntaxError(\"Unexpected token '\" + c + \"' in JSON\");\n            }\n        }\n\n        private Map<String, Object> parseObject() {\n            // we know s.charAt(pos) == '{'\n            pos++;\n            Map<String, Object> map = new LinkedHashMap<>();\n            skipWs();\n            if (pos < len && s.charAt(pos) == '}') {\n                pos++;\n                return map;\n            }\n            while (true) {\n                skipWs();\n                if (pos >= len || s.charAt(pos) != '\"') {\n                    throw syntaxError(\"Expected string key in object\");\n                }\n                String key = parseString();","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsonParser.java#L81-L117","documentation":"The JSON parser encountered a character that cannot start a JSON value where one was required. After switching on `{`, `[`, `\"`, and literal starts, the default branch allows only `-` or digits for numbers; anything else triggers \"Unexpected token '<c>' in JSON\". This mirrors JSON.parse's own error style.","triggerScenarios":"Parsing input like `\"undefined\"`, `\"{'a':1}\"` (single quotes), `\"NaN\"`, or stray text before/after a JSON document, where the offending character reaches parseValue's default branch.","commonSituations":"Passing JS literals (undefined, NaN, single-quoted objects) where strict JSON is required; response bodies that contain HTML error pages; copy-pasted JSON with smart quotes or BOM/leading garbage.","solutions":["Quote strings with double quotes and use only JSON literals (null, true, false, numbers) in the input.","Strip any non-JSON prefix/suffix (e.g. HTML, BOM, log lines) before parsing.","Validate the payload with a linter or JSON.parse in a test to confirm it is strict JSON."],"exampleFix":"// before\nJSON.parse(\"{'name': 'a'}\");\n\n// after\nJSON.parse('{\"name\": \"a\"}');","handlingStrategy":"validation","validationCode":"// cheap pre-check for obviously non-JSON starts\nvar t = input.trim();\nif (!(t.startsWith('{') || t.startsWith('[') || t.startsWith('\"') || /^-?[0-9tfn]/.test(t))) {\n    throw new IllegalArgumentException('input does not look like JSON: ' + t.substring(0, 20));\n}","typeGuard":null,"tryCatchPattern":"try {\n    var obj = JSON.parse(input);\n} catch (e) {\n    if (('' + e).indexOf('Unexpected token') >= 0) {\n        console.log('non-JSON content near start of: ' + input.substring(0, 40));\n    }\n    throw e;\n}","preventionTips":["Use strict JSON syntax: double quotes, no undefined/NaN/single quotes.","Strip HTML error pages or BOM before parsing response bodies.","Round-trip generated JSON through a serializer in tests."],"tags":["json","parser","syntax-error"],"backgroundTag":"json-parse-error","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}