karatelabs/karate · error · RuntimeException

invalid json: input is null or blank

Error message

invalid json: input is null or blank

What it means

parseLenient() is Karate's forgiving JSON parser wrapper; before attempting a parse it rejects null or blank input with this RuntimeException. Lenient parsing tolerates loose syntax but cannot do anything with no input at all.

Solutions

  1. Validate the string is non-blank before parsing
  2. Return a default ("{}"/"[]") or throw a domain-specific error in your code instead
  3. Trace and fix the source of the blank string

Example fix

// before
Object o = Json.parseLenient(fileContent);
// after
if (fileContent == null || fileContent.isBlank()) throw new IllegalStateException("fixture file is empty");
Object o = Json.parseLenient(fileContent);
Defensive patterns

Strategy: validation

Validate before calling

if (json == null || json.isBlank()) { throw new IllegalArgumentException("cannot parse blank json input"); }

Try / catch

try { Object o = Json.parseLenient(s); } catch (RuntimeException e) { if ("invalid json: input is null or blank".equals(e.getMessage())) { o = new java.util.HashMap<>(); } else { throw e; } }

Prevention

When it happens

Trigger: Calling Json.parseLenient(null), parseLenient(""), parseLenient(" "), or indirectly via Json.of(blankString).

Common situations: Reading empty fixture files, uninitialized string variables, or blank secrets/config entries.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/common/Json.java:123

        if (any == null) {
            throw new IllegalArgumentException("input must not be null");
        }
        if (any instanceof String s) {
            if (s.isBlank()) {
                throw new IllegalArgumentException("input string must not be empty or blank");
            }
            return new Json(JsonPath.parse(parseLenient(s)));
        } else if (any instanceof List || any instanceof Map) {
            return new Json(JsonPath.parse(any));
        } else {
            String json = JSONValue.toJSONString(any);
            return new Json(JsonPath.parse(json));
        }
    }

    public static Object parseLenient(String json) {
        if (json == null || json.isBlank()) {
            throw new RuntimeException("invalid json: input is null or blank");
        }
        try {
            Object result = JSONValue.parseKeepingOrder(json);
            if (!isMapOrList(result)) {
                throw new RuntimeException("invalid json: not a JSON object or array");
            }
            return result;
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("invalid json: " + e.getMessage(), e);
        }
    }

    public static boolean isMapOrList(Object o) {
        return o instanceof Map || o instanceof List;
    }

View on GitHub (pinned to a22eb90246)