apache/cassandra · error · InvalidRequestException

Could not decode JSON string as a map: %s. (String was: %s)

Error message

Could not decode JSON string as a map: %s. (String was: %s)

What it means

Json.parseJson catches IOException from the Jackson ObjectMapper and rethrows it as this InvalidRequestException, meaning the string could not be decoded as a JSON map at all. The original exception toString and the offending input string are included for diagnosis.

Source

Thrown at src/java/org/apache/cassandra/cql3/Json.java:319

                    }
                    catch (MarshalException exc)
                    {
                        throw new InvalidRequestException(format("Error decoding JSON value for %s: %s", spec.name, exc.getMessage()));
                    }
                }
            }

            if (!valueMap.isEmpty())
            {
                throw new InvalidRequestException(format("JSON values map contains unrecognized column: %s",
                                                         valueMap.keySet().iterator().next()));
            }

            return columnMap;
        }
        catch (IOException exc)
        {
            throw new InvalidRequestException(format("Could not decode JSON string as a map: %s. (String was: %s)", exc.toString(), jsonString));
        }
        catch (MarshalException exc)
        {
            throw new InvalidRequestException(exc.getMessage());
        }
    }
}

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Validate the JSON string with a JSON parser/validator before executing the statement
  2. Fix syntax: double quotes for keys and strings, no trailing commas, no single quotes
  3. If the value is a Java object, serialize it with a JSON library rather than string concatenation
  4. Check upstream producers (queues, files, APIs) for truncated or corrupted payloads

Example fix

// before
String json = "{'a': 1,}";
// after
String json = "{\"a\": 1}";
Defensive patterns

Strategy: validation

Validate before calling

private static void requireValidJson(String json) {
    try { new ObjectMapper().readTree(json); }
    catch (IOException e) { throw new IllegalArgumentException("Not valid JSON: " + e.getMessage(), e); }
}

Try / catch

try { session.execute("INSERT INTO t JSON ?", json); } catch (InvalidRequestException e) { if (e.getMessage().startsWith("Could not decode JSON string")) { log.error("Bad JSON: {}", json, e); throw new IllegalArgumentException("Malformed JSON payload", e); } throw e; }

Prevention

When it happens

Trigger: Passing syntactically invalid JSON to INSERT INTO t JSON '...' or a bound JSON parameter: unquoted keys, trailing commas, single quotes, truncated documents, or non-JSON text.

Common situations: Hand-writing JSON with single-quoted strings; concatenating payloads that truncate; logs/templating injecting garbage; reading the JSON from a file or queue with corruption; passing a Java Map's toString() instead of real JSON.

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 apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/e56b5fc6d7702028. Report an issue: GitHub.