karatelabs/karate · error · RuntimeException

Invalid config: expected JSON object

Error message

Invalid config: expected JSON object

What it means

KaratePom.parse(json) requires the karate-pom configuration to be a JSON object; if the supplied text parses as anything other than an object (array, string, number, or invalid JSON surfacing as a non-object) it throws this error.

Solutions

  1. Ensure the config root is a JSON object starting with { and ending with }
  2. Validate the file with a JSON linter/parser before use
  3. Check for empty or truncated files and restore full content
  4. Convert YAML/other formats to JSON

Example fix

// before (karate-pom.json)
["src/test/java"]
// after
{
  "paths": ["src/test/java"]
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate JSON root is an object before parsing
Json j = Json.of(Files.readString(path));
if (!j.isObject()) throw new IllegalStateException("karate-pom.json root must be a JSON object");

Type guard

static boolean isJsonObject(String json) { Json j = Json.of(json); return j.isObject(); }

Try / catch

try {
    KaratePom pom = KaratePom.parse(json);
} catch (RuntimeException e) {
    throw new IllegalStateException("karate-pom must be a JSON object, got: " + json.getClass(), e);
}

Prevention

When it happens

Trigger: Passing a JSON array, bare scalar, empty string, or otherwise non-object content to KaratePom.parse() — directly or via load() of a malformed karate-pom.json.

Common situations: Config file accidentally saved as an array of settings or containing only a comment; file truncated to empty; pasting YAML instead of JSON; BOM or encoding garbage corrupting the JSON.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KaratePom.java:197

        try {
            String content = Files.readString(configPath);
            return parse(content);
        } catch (Exception e) {
            throw new RuntimeException("Failed to load config from: " + configPath, e);
        }
    }

    /**
     * Parse configuration from a JSON string.
     *
     * @param json JSON string
     * @return parsed KaratePom
     * @throws RuntimeException if JSON is invalid
     */
    public static KaratePom parse(String json) {
        Json j = Json.of(json);
        if (!j.isObject()) {
            throw new RuntimeException("Invalid config: expected JSON object");
        }
        KaratePom config = new KaratePom();

        // Parse paths
        j.<List<String>>getOptional("paths").ifPresent(config::setPaths);

        // Parse tags
        j.<List<String>>getOptional("tags").ifPresent(config::setTags);

        // Parse simple fields
        j.<String>getOptional("env").ifPresent(config::setEnv);
        j.<Integer>getOptional("threads").ifPresent(config::setThreads);
        j.<String>getOptional("scenarioName").ifPresent(config::setScenarioName);
        j.<String>getOptional("configDir").ifPresent(config::setConfigDir);
        j.<Boolean>getOptional("dryRun").ifPresent(config::setDryRun);
        j.<Boolean>getOptional("clean").ifPresent(config::setClean);
        j.<String>getOptional("workingDir").ifPresent(config::setWorkingDir);

View on GitHub (pinned to a22eb90246)