{"record":{"id":"3d47d8ff666a6a5c","repo":"apache/cassandra","slug":"value-for-column-s-violated-s-constraint-as-it","errorCode":null,"errorMessage":"Value for column '%s' violated %s constraint as it is not a valid JSON.","messagePattern":"Value for column '(.+?)' violated (.+?) constraint as it is not a valid JSON\\.","errorType":"validation","errorClass":"ConstraintViolationException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/constraints/JsonConstraint.java","lineNumber":58,"sourceCode":"    {\n        super(name, args);\n    }\n\n    public JsonConstraint(List<String> args)\n    {\n        this(FUNCTION_NAME, args);\n    }\n\n    @Override\n    public void internalEvaluate(AbstractType<?> valueType, Operator relationType, String term, ByteBuffer columnValue)\n    {\n        try\n        {\n            JsonUtils.decodeJson(valueType.getString(columnValue));\n        }\n        catch (MarshalException ex)\n        {\n            throw new ConstraintViolationException(format(\"Value for column '%s' violated %s constraint as it is not a valid JSON.\",\n                                                          columnName,\n                                                          name));\n        }\n    }\n\n    @Override\n    public List<AbstractType<?>> getSupportedTypes()\n    {\n        return SUPPORTED_TYPES;\n    }\n\n    @Override\n    public boolean equals(Object o)\n    {\n        if (this == o)\n            return true;\n\n        if (!(o instanceof JsonConstraint))","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/constraints/JsonConstraint.java#L40-L76","documentation":"JsonConstraint.internalEvaluate validates at write time that the column value is well-formed JSON by decoding the string via JsonUtils.decodeJson; on MarshalException it throws ConstraintViolationException naming the column and the constraint. This guarantees JSON-constrained columns only ever store parseable JSON.","triggerScenarios":"INSERT/UPDATE writing a string that fails JSON parsing to a column with a JSON constraint — e.g. `payload text ... CHECK JSON()` receiving `{invalid` or plain text like `hello`.","commonSituations":"Applications storing concatenated/truncated JSON; single quotes vs double quotes (e.g. `{'a': 1}` is invalid JSON); encoding issues; users assuming Cassandra validates JSON automatically without the constraint.","solutions":["Serialize the value with a proper JSON encoder before writing (e.g. Jackson ObjectMapper.writeValueAsString).","Validate the JSON string client-side before the write (JSON.parse / json.loads).","Remove the JSON constraint if arbitrary text should be stored."],"exampleFix":"// before\nString payload = \"{'key': 'value'}\"; // invalid JSON\n// after\nString payload = new ObjectMapper().writeValueAsString(Map.of(\"key\", \"value\")); // {\"key\":\"value\"}","handlingStrategy":"try-catch","validationCode":"// client-side pre-validation\ntry { new com.fasterxml.jackson.databind.ObjectMapper().readValue(payload, Object.class); }\ncatch (Exception e) { throw new IllegalArgumentException(\"payload is not valid JSON\", e); }","typeGuard":null,"tryCatchPattern":"try {\n    session.execute(write);\n} catch (ConstraintViolationException e) {\n    if (e.getMessage().contains(\"not a valid JSON\")) {\n        // fix serialization or store as plain text\n    } else throw e;\n}","preventionTips":["Always serialize with a JSON library, never string concatenation","Remember JSON requires double quotes for keys/strings","Add unit tests asserting payloads parse as JSON before writing"],"tags":["cql","write-path","constraints","json"],"backgroundTag":"json-parse-error","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}