apache/cassandra · error · InvalidTypeException

Malformed map value "%s", missing closing '}'

Error message

Malformed map value "%s", missing closing '}'

What it means

Thrown by MapCodec.parse() when the literal string ends after parsing at least one entry but the closing '}' was never found — unbalanced braces. Like the collection equivalent, the value is truncated and cannot be a valid CQL map literal.

Source

Thrown at src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java:2423

                    e);
                }

                V v = valueCodec.parse(value.substring(idx, n));
                idx = n;

                m.put(k, v);

                idx = ParseUtils.skipSpaces(value, idx);
                if (value.charAt(idx) == '}') return m;
                if (value.charAt(idx++) != ',')
                    throw new InvalidTypeException(
                    String.format(
                    "Cannot parse map value from \"%s\", at character %d expecting ',' but got '%c'",
                    value, idx, value.charAt(idx)));

                idx = ParseUtils.skipSpaces(value, idx);
            }
            throw new InvalidTypeException(
            String.format("Malformed map value \"%s\", missing closing '}'", value));
        }

        @Override
        public String format(Map<K, V> value)
        {
            if (value == null) return "NULL";
            StringBuilder sb = new StringBuilder();
            sb.append('{');
            int i = 0;
            for (Map.Entry<K, V> e : value.entrySet())
            {
                if (i++ != 0) sb.append(',');
                sb.append(keyCodec.format(e.getKey()));
                sb.append(':');
                sb.append(valueCodec.format(e.getValue()));
            }
            sb.append('}');

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Append the missing '}' to complete the map literal
  2. Fix element quoting so the closing brace is not consumed inside a quoted token
  3. Pre-validate brace balance before calling parse()
  4. Use codec.format() to produce well-formed literals

Example fix

// before
mapCodec.parse("{1:'a',2:'b'");
// after
mapCodec.parse("{1:'a',2:'b'}");
Defensive patterns

Strategy: validation

Validate before calling

String t = value == null ? null : value.trim();
if (t == null || !t.startsWith("{") || !t.endsWith("}"))
    throw new IllegalArgumentException("Unbalanced map literal: " + value);
mapCodec.parse(value);

Type guard

boolean isClosedMap(String s) {
    String t = s == null ? "" : s.trim();
    int depth = 0; boolean q = false;
    for (char c : t.toCharArray()) {
        if (c == '\'') q = !q;
        else if (!q) { if (c == '{') depth++; if (c == '}') depth--; }
    }
    return depth == 0 && !q;
}

Try / catch

try {
    Map<K,V> m = mapCodec.parse(value);
} catch (InvalidTypeException e) {
    if (e.getMessage().contains("missing closing"))
        value = value + "}"; // attempt repair, then re-validate
    throw e;
}

Prevention

When it happens

Trigger: Calling MapCodec.parse() with a truncated map literal like "{1:'a',2:'b'" — all entries parse but end-of-string is reached without '}'.

Common situations: Substring/log-truncation cutting off the final brace; user input missing the closing brace; quoted value tokens swallowing the closing brace due to unescaped quotes.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/ca59bc5acdb93896. Report an issue: GitHub.