{"record":{"id":"d17eb40c97f6d564","repo":"karatelabs/karate","slug":"invalid-number-bare","errorCode":null,"errorMessage":"Invalid number: bare '-'","messagePattern":"Invalid number: bare '-'","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsonParser.java","lineNumber":261,"sourceCode":"                char h = s.charAt(pos + i);\n                int d;\n                if (h >= '0' && h <= '9') d = h - '0';\n                else if (h >= 'a' && h <= 'f') d = 10 + (h - 'a');\n                else if (h >= 'A' && h <= 'F') d = 10 + (h - 'A');\n                else throw syntaxError(\"Invalid hex digit '\" + h + \"' in \\\\u escape\");\n                v = (v << 4) | d;\n            }\n            pos += 4;\n            return (char) v;\n        }\n\n        private Object parseNumber() {\n            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++;","sourceCodeStart":243,"sourceCodeEnd":279,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsonParser.java#L243-L279","documentation":"Karate's internal JSON tokenizer (JsonParser.parseNumber) rejects a number token that begins with '-' but has no digits after it. JSON numbers may be negative, but a lone minus sign is not a valid number literal. The parser checks the character after '-' and throws a syntax error if the input ends or a non-numeric token follows.","triggerScenarios":"Calling the JSON parser (e.g. Json.of(...)/Json.parse on a string) with input where a value position contains '-' immediately followed by end-of-input or a delimiter, e.g. '[1, -]', '{\"a\": -}' or a trailing '-' after whitespace trimming.","commonSituations":"Hand-edited JSON config files where a negative value was never filled in; template/placeholder substitution that left '-' behind; string concatenation building JSON where a variable defaulted to empty; truncated responses ending mid-token.","solutions":["Inspect the JSON string at the reported position and replace the bare '-' with a valid number (e.g. 0 or the intended negative value).","If the JSON is built dynamically, ensure numeric variables are never interpolated as empty/null and quote placeholders that may be absent.","Validate the payload with a JSON linter/parser before passing it to Karate to get a clearer diff of the malformed spot.","If the value is genuinely optional, emit 'null' instead of '-' for missing numbers."],"exampleFix":"// before\nString json = \"{\\\"amount\\\": \" + amount + \"}\"; // amount == \"\" -> {\"amount\": -}\n// after\nString lit = (amount == null || amount.isEmpty()) ? \"null\" : amount;\nString json = \"{\\\"amount\\\": \" + lit + \"}\";","handlingStrategy":"validation","validationCode":"// Java: check the payload parses strictly before use\ntry (var p = new java.io.PushbackReader(new java.io.StringReader(json))) { /* or */ }\n// simplest guard:\nboolean valid = com.jayway.jsonpath.JsonPath.parse(json) != null; // or pre-scan for bare '-':\nif (json.matches(\".*[^0-9\\\\.eE]-\\\\s*[,}\\\\]].*\") || json.trim().endsWith(\"-\")) {\n    throw new IllegalArgumentException(\"bare '-' is not a valid JSON number\");\n}","typeGuard":null,"tryCatchPattern":"// Java\ntry {\n    Json json = Json.of(raw);\n} catch (Exception e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Invalid number\")) {\n        // sanitize placeholder tokens and retry\n        raw = raw.replaceAll(\"-\\\\s*([,}\\\\]]|$)\", \"null$1\");\n    } else { throw e; }\n}","preventionTips":["Never build JSON by string concatenation; use a serializer or Karate's Json object.","Default missing numeric values to null or 0 in templates.","Run generated payloads through a JSON linter in CI.","Check for truncation when payloads cross size boundaries."],"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"}