{"record":{"id":"4e2fd907ecf2e7ba","repo":"apache/cassandra","slug":"error-decoding-json-bytes","errorCode":null,"errorMessage":"Error decoding JSON bytes: ","messagePattern":"Error decoding JSON bytes: ","errorType":"exception","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/utils/JsonUtils.java","lineNumber":89,"sourceCode":"\n    /**\n     * Quotes string contents using standard JSON quoting.\n     */\n    public static String quoteAsJsonString(String s)\n    {\n        // In future should update to directly use `JsonStringEncoder.getInstance()` but for now:\n        return new String(BufferRecyclers.getJsonStringEncoder().quoteAsString(s));\n    }\n\n    public static Object decodeJson(byte[] json)\n    {\n        try\n        {\n            return JSON_OBJECT_MAPPER.readValue(json, Object.class);\n        }\n        catch (IOException ex)\n        {\n            throw new MarshalException(\"Error decoding JSON bytes: \" + ex.getMessage());\n        }\n    }\n\n    public static Object decodeJson(String json)\n    {\n        try\n        {\n            return JSON_OBJECT_MAPPER.readValue(json, Object.class);\n        }\n        catch (IOException ex)\n        {\n            throw new MarshalException(\"Error decoding JSON string: \" + ex.getMessage());\n        }\n    }\n\n    public static byte[] writeAsJsonBytes(Object value)\n    {\n        try","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/utils/JsonUtils.java#L71-L107","documentation":"JsonUtils.decodeJson(byte[]) parses JSON bytes with Jackson's ObjectMapper. If the bytes are not valid JSON (malformed syntax, wrong encoding, or an I/O problem), it rethrows as MarshalException 'Error decoding JSON bytes: <message>'.","triggerScenarios":"Calling JsonUtils.decodeJson(byte[]) with bytes that are truncated, corrupted, in a non-UTF-8 encoding, or not JSON at all.","commonSituations":"Reading JSON blobs from storage that were written by another tool or truncated; deserializing CQL JSON columns with binary garbage; charset mismatches.","solutions":["Verify the byte content is valid UTF-8 and complete JSON (e.g. dump as a String to inspect)","Ensure the producer wrote the JSON with JsonUtils.writeAsJsonBytes or equivalent standard encoder","Catch MarshalException at the call site and handle the corrupt payload"],"exampleFix":"// before\nObject v = JsonUtils.decodeJson(bytes);\n// after\ntry {\n    Object v = JsonUtils.decodeJson(bytes);\n} catch (MarshalException e) {\n    logger.warn(\"Corrupt JSON payload: {}\", e.getMessage());\n}","handlingStrategy":"try-catch","validationCode":"// sanity check before decode\nif (bytes == null || bytes.length == 0) throw new IllegalArgumentException(\"empty JSON payload\");","typeGuard":null,"tryCatchPattern":"try { return JsonUtils.decodeJson(bytes); } catch (MarshalException e) { throw new InvalidRequestException(\"Invalid JSON payload: \" + e.getMessage()); }","preventionTips":["Always write JSON payloads with JsonUtils.writeAsJsonBytes so round-trips are safe","Validate payloads at ingest with a JSON schema or parser check","Log a hex/text preview of failing bytes to diagnose encoding problems"],"tags":["json","jackson","decoding"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}