karatelabs/karate · error

Invalid number

Error message

Invalid number

What it means

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.

Solutions

  1. Rewrite the number literal in strict JSON form: '.5' -> '0.5', '+5' -> '5', 'NaN'/'Infinity' -> 'null' or a numeric sentinel.
  2. Check the character at the reported parse offset; it is the invalid start of the number token.
  3. Sanitize generated JSON: run values through a proper serializer instead of string concatenation.
  4. Validate with a strict JSON parser before handing the string to Karate.

Example fix

// before
String json = "{\"score\": .75}";
// after
String json = "{\"score\": 0.75}";
Defensive patterns

Strategy: validation

Validate before calling

// reject non-JSON number starts before parsing
java.util.regex.Pattern BAD_NUM = java.util.regex.Pattern.compile("[:\\[,]\\s*(\\.\\d|\\+\\d|NaN|Infinity)");
if (BAD_NUM.matcher(json).find()) {
    throw new IllegalArgumentException("non-JSON number literal found (e.g. .5, +5, NaN)");
}

Try / catch

// Java
try {
    Json json = Json.of(raw);
} catch (Exception e) {
    if (String.valueOf(e.getMessage()).contains("Invalid number")) {
        raw = raw.replace("NaN", "null").replace("Infinity", "null").replaceAll("([:,\\[])\\s*\\.(\\d)", "$1 0.$2");
    } else { throw e; }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/d779d457033d1b80. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsonParser.java:274

            int start = pos;
            boolean isFloat = false;
            if (s.charAt(pos) == '-') {
                pos++;
                if (pos >= len) {
                    throw syntaxError("Invalid number: bare '-'");
                }
            }
            // integer part
            char c = s.charAt(pos);
            if (c == '0') {
                pos++;
            } else if (c >= '1' && c <= '9') {
                pos++;
                while (pos < len && (s.charAt(pos) >= '0' && s.charAt(pos) <= '9')) {
                    pos++;
                }
            } else {
                throw syntaxError("Invalid number");
            }
            // fraction
            if (pos < len && s.charAt(pos) == '.') {
                isFloat = true;
                pos++;
                int fracStart = pos;
                while (pos < len && (s.charAt(pos) >= '0' && s.charAt(pos) <= '9')) {
                    pos++;
                }
                if (pos == fracStart) {
                    throw syntaxError("Invalid number: missing fraction digits");
                }
            }
            // exponent
            if (pos < len && (s.charAt(pos) == 'e' || s.charAt(pos) == 'E')) {
                isFloat = true;
                pos++;
                if (pos < len && (s.charAt(pos) == '+' || s.charAt(pos) == '-')) {

View on GitHub (pinned to a22eb90246)