karatelabs/karate · error

Invalid literal — expected 'true'

Error message

Invalid literal — expected 'true'

What it means

JsonParser.parseBoolean saw a token starting with 't' that is not exactly the four characters 'true'. The parser only accepts exact JSON literals — abbreviations like 'True', 'tru', or 'truthy' are rejected.

Solutions

  1. Replace the literal with lowercase 'true'.
  2. Fix the producing side to serialize booleans with JSON semantics (JSON.stringify, json.dumps with proper encoder) rather than language reprs.
  3. For Python sources use json.dumps instead of str()/repr() so True becomes true.
  4. Normalize case-sensitive boolean strings before parsing when converting config formats.

Example fix

// before
String json = "{\"enabled\": True}"; // Python repr
// after
String json = "{\"enabled\": true}";
Defensive patterns

Strategy: validation

Validate before calling

// normalize non-JSON booleans before parsing
raw = raw.replaceAll("\\bTrue\\b", "true").replaceAll("\\bTRUE\\b", "true");
// flag remaining suspects:
if (raw.matches(".*\\btru?[a-z]*\\b(?!e\\s*[: Deadline,}\\]]).*")) { /* inspect */ }

Try / catch

// Java
try {
    Json json = Json.of(raw);
} catch (Exception e) {
    if (String.valueOf(e.getMessage()).contains("expected 'true'")) {
        raw = raw.replaceAll("\\b(True|TRUE)\\b", "true");
    } else { throw e; }
}

Prevention

When it happens

Trigger: Parsing JSON where a boolean value is 'True' (Python style), 'TRUE' (SQL/INI style), or a misspelled/shortened variant beginning with lowercase 't', e.g. '{"flag": tru}'.

Common situations: JSON generated from Python (repr of True), config files converted from properties/INI that used TRUE, typos in hand-written JSON, templating that emitted a language-specific boolean.

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/9990eedccf68841e. Report an issue: GitHub.

Appendix: source

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

            // karate-core stable (OAuth2Token.fromMap, W3cDriver, etc.).
            try {
                long v = Long.parseLong(lit);
                if (v >= Integer.MIN_VALUE && v <= Integer.MAX_VALUE) {
                    return (int) v;
                }
                return v;
            } catch (NumberFormatException nfe) {
                return new BigInteger(lit);
            }
        }

        private Boolean parseBoolean() {
            if (s.charAt(pos) == 't') {
                if (pos + 4 <= len && s.charAt(pos + 1) == 'r' && s.charAt(pos + 2) == 'u' && s.charAt(pos + 3) == 'e') {
                    pos += 4;
                    return Boolean.TRUE;
                }
                throw syntaxError("Invalid literal — expected 'true'");
            }
            if (pos + 5 <= len && s.charAt(pos + 1) == 'a' && s.charAt(pos + 2) == 'l' && s.charAt(pos + 3) == 's' && s.charAt(pos + 4) == 'e') {
                pos += 5;
                return Boolean.FALSE;
            }
            throw syntaxError("Invalid literal — expected 'false'");
        }

        private Object parseNull() {
            if (pos + 4 <= len && s.charAt(pos + 1) == 'u' && s.charAt(pos + 2) == 'l' && s.charAt(pos + 3) == 'l') {
                pos += 4;
                return null;
            }
            throw syntaxError("Invalid literal — expected 'null'");
        }

        void skipWs() {
            while (pos < len) {

View on GitHub (pinned to a22eb90246)