{"record":{"id":"d779d457033d1b80","repo":"karatelabs/karate","slug":"invalid-number","errorCode":null,"errorMessage":"Invalid number","messagePattern":"Invalid number","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsonParser.java","lineNumber":274,"sourceCode":"            int start = pos;\n            boolean isFloat = false;\n            if (s.charAt(pos) == '-') {\n                pos++;\n                if (pos >= len) {\n                    throw syntaxError(\"Invalid number: bare '-'\");\n                }\n            }\n            // integer part\n            char c = s.charAt(pos);\n            if (c == '0') {\n                pos++;\n            } else if (c >= '1' && c <= '9') {\n                pos++;\n                while (pos < len && (s.charAt(pos) >= '0' && s.charAt(pos) <= '9')) {\n                    pos++;\n                }\n            } else {\n                throw syntaxError(\"Invalid number\");\n            }\n            // fraction\n            if (pos < len && s.charAt(pos) == '.') {\n                isFloat = true;\n                pos++;\n                int fracStart = pos;\n                while (pos < len && (s.charAt(pos) >= '0' && s.charAt(pos) <= '9')) {\n                    pos++;\n                }\n                if (pos == fracStart) {\n                    throw syntaxError(\"Invalid number: missing fraction digits\");\n                }\n            }\n            // exponent\n            if (pos < len && (s.charAt(pos) == 'e' || s.charAt(pos) == 'E')) {\n                isFloat = true;\n                pos++;\n                if (pos < len && (s.charAt(pos) == '+' || s.charAt(pos) == '-')) {","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsonParser.java#L256-L292","documentation":"JsonParser.parseNumber throws this when the character starting a number is not a digit ('0'-'9') and not a valid continuation. JSON numbers must have at least one integer digit; tokens like '.5', '+5', 'NaN', or an identifier where a number is expected are rejected.","triggerScenarios":"Parsing JSON where a value begins with an invalid character for a number — e.g. '.5' (leading dot), '+5' (leading plus), '01x' style garbage, or any non-digit char that parseValue dispatched to parseNumber.","commonSituations":"JSON copied from JavaScript source using '+5' or '.5' shorthand; values like 'Infinity' or 'NaN' produced by JSON.stringify replacements; paste corruption where the first digit was dropped.","solutions":["Rewrite the number literal in strict JSON form: '.5' -> '0.5', '+5' -> '5', 'NaN'/'Infinity' -> 'null' or a numeric sentinel.","Check the character at the reported parse offset; it is the invalid start of the number token.","Sanitize generated JSON: run values through a proper serializer instead of string concatenation.","Validate with a strict JSON parser before handing the string to Karate."],"exampleFix":"// before\nString json = \"{\\\"score\\\": .75}\";\n// after\nString json = \"{\\\"score\\\": 0.75}\";","handlingStrategy":"validation","validationCode":"// reject non-JSON number starts before parsing\njava.util.regex.Pattern BAD_NUM = java.util.regex.Pattern.compile(\"[:\\\\[,]\\\\s*(\\\\.\\\\d|\\\\+\\\\d|NaN|Infinity)\");\nif (BAD_NUM.matcher(json).find()) {\n    throw new IllegalArgumentException(\"non-JSON number literal found (e.g. .5, +5, NaN)\");\n}","typeGuard":null,"tryCatchPattern":"// Java\ntry {\n    Json json = Json.of(raw);\n} catch (Exception e) {\n    if (String.valueOf(e.getMessage()).contains(\"Invalid number\")) {\n        raw = raw.replace(\"NaN\", \"null\").replace(\"Infinity\", \"null\").replaceAll(\"([:,\\\\[])\\\\s*\\\\.(\\\\d)\", \"$1 0.$2\");\n    } else { throw e; }\n}","preventionTips":["Serialize numbers with a JSON encoder, never String.valueOf on raw values.","Remember JSON disallows leading '+', leading '.', NaN, and Infinity.","Test parsers with edge-case numeric literals in unit tests.","Validate third-party JSON with a strict parser before ingestion."],"tags":["json","parser","syntax-error","number-literal"],"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"}