karatelabs/karate · error

Expected ',' or '}' in object

Error message

Expected ',' or '}' in object

What it means

Thrown by JsonParser.parseObject when, after a complete key/value pair, the next non-whitespace character is neither ',' (next pair) nor '}' (end of object). The input is structurally malformed inside an object — e.g. two values separated by something other than a comma. RFC 8259 allows only ',' or '}' at that position, so the parser fails fast.

Solutions

  1. Look at the character position reported by the error and insert the missing ',' between the object's members.
  2. Remove any stray characters after the last valid key/value pair.
  3. Build JSON via structured serialization (map building, a JSON library) instead of string concatenation.
  4. Lint the JSON with a standard tool before parsing to catch the malformed pair early.

Example fix

// before
'{"a": 1 "b": 2}'
// after
'{"a": 1, "b": 2}'
Defensive patterns

Strategy: validation

Validate before calling

// Reject objects with missing commas: parse with a standard parser first
try {
    new com.fasterxml.jackson.databind.ObjectMapper().readTree(json);
} catch (JsonProcessingException e) {
    throw new IllegalArgumentException("JSON object separators invalid: " + e.getOriginalMessage());
}

Type guard

static boolean hasBalancedObjectSyntax(String s) {
    return s != null && s.trim().matches("\\{.*\}");
} // plus a real parse check before trusting it

Try / catch

try {
    Object o = Json.parse(raw);
} catch (JsonSyntaxException e) {
    int pos = e.getPosition();
    throw new IllegalArgumentException("Bad JSON near offset " + pos + ": expected ',' or '}'");
}

Prevention

When it happens

Trigger: Parsing input like '{"a":1 "b":2}' (missing comma between pairs) or '{"a":1 b}' — a stray character appears where ',' or '}' is required.

Common situations: Hand-edited JSON with a missing comma between properties, concatenating two JSON objects without a comma, generating JSON by naive string concatenation, accidentally pasting text inside a JSON object.

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

Appendix: source

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

                // RFC 8259: behavior of duplicate keys is unspecified; we keep
                // the last value (matches json-smart parseKeepingOrder).
                map.put(key, value);
                skipWs();
                if (pos >= len) {
                    throw syntaxError("Unexpected end of JSON input in object");
                }
                char c = s.charAt(pos);
                if (c == ',') {
                    pos++;
                    // trailing-comma is invalid per spec: the next iteration
                    // demands a string key, which rejects e.g. {"a":1,}.
                    continue;
                }
                if (c == '}') {
                    pos++;
                    return map;
                }
                throw syntaxError("Expected ',' or '}' in object");
            }
        }

        private List<Object> parseArray() {
            // we know s.charAt(pos) == '['
            pos++;
            List<Object> list = new ArrayList<>();
            skipWs();
            if (pos < len && s.charAt(pos) == ']') {
                pos++;
                return list;
            }
            while (true) {
                skipWs();
                list.add(parseValue());
                skipWs();
                if (pos >= len) {
                    throw syntaxError("Unexpected end of JSON input in array");

View on GitHub (pinned to a22eb90246)