karatelabs/karate · error · IllegalArgumentException

input string must not be empty or blank

Error message

input string must not be empty or blank

What it means

When Json.of() receives a String it must contain actual JSON content; a null, empty, or whitespace-only string cannot be parsed into a document, so it throws immediately. This is distinct from malformed JSON — the string has no content at all.

Solutions

  1. Guard with `s != null && !s.isBlank()` before calling Json.of
  2. Provide a default JSON string like "{}" or "[]" when input is blank
  3. Fix the source producing the blank string (file/env/response)

Example fix

// before
Json json = Json.of(configString);
// after
Json json = (configString == null || configString.isBlank()) ? Json.of("{}") : Json.of(configString);
Defensive patterns

Strategy: validation

Validate before calling

if (s == null || s.isBlank()) { s = "{}"; }

Type guard

boolean isNonBlankString(Object o) { return o instanceof String s && !s.isBlank(); }

Try / catch

try { Json json = Json.of(str); } catch (IllegalArgumentException e) { if (e.getMessage().contains("must not be empty or blank")) { json = Json.of("{}"); } else { throw e; } }

Prevention

When it happens

Trigger: Json.of("") or Json.of(" "), or Json.of(someStringVariable) where the string came from an empty file, blank env var, or empty response body.

Common situations: Empty config files, blank environment variables, or HTTP 204/empty-body responses fed into Json.of().

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

Appendix: source

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

    private String prefix(String path) {
        return path.charAt(0) == '$' ? path : prefix + path;
    }

    public static Json object() {
        return new Json(JsonPath.parse("{}"));
    }

    public static Json array() {
        return new Json(JsonPath.parse("[]"));
    }

    public static Json of(Object any) {
        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");

View on GitHub (pinned to a22eb90246)