{"record":{"id":"2887b2787f692777","repo":"apache/cassandra","slug":"got-null-for-insert-json-values","errorCode":null,"errorMessage":"Got null for INSERT JSON values","messagePattern":"Got null for INSERT JSON values","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/Json.java","lineNumber":278,"sourceCode":"        }\n\n        @Override\n        public void addFunctionsTo(List<Function> functions)\n        {\n        }\n    }\n\n    /**\n     * Given a JSON string, return a map of columns to their values for the insert.\n     */\n    static Map<ColumnIdentifier, Term> parseJson(String jsonString, Collection<ColumnMetadata> expectedReceivers)\n    {\n        try\n        {\n            Map<String, Object> valueMap = JsonUtils.JSON_OBJECT_MAPPER.readValue(jsonString, Map.class);\n\n            if (valueMap == null)\n                throw new InvalidRequestException(\"Got null for INSERT JSON values\");\n\n            JsonUtils.handleCaseSensitivity(valueMap);\n\n            Map<ColumnIdentifier, Term> columnMap = new HashMap<>(expectedReceivers.size());\n            for (ColumnSpecification spec : expectedReceivers)\n            {\n                // We explicitely test containsKey() because the value itself can be null, and we want to distinguish an\n                // explicit null value from no value\n                if (!valueMap.containsKey(spec.name.toString()))\n                    continue;\n\n                Object parsedJsonObject = valueMap.remove(spec.name.toString());\n                if (parsedJsonObject == null)\n                {\n                    // This is an explicit user null\n                    columnMap.put(spec.name, Constants.NULL_VALUE);\n                }\n                else","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/Json.java#L260-L296","documentation":"Json.parseJson throws this InvalidRequestException when the JSON string supplied to INSERT JSON (or ... AS JSON paths) deserializes to a JSON null instead of an object. Cassandra requires a JSON object mapping column names to values; a literal 'null' document carries no column data.","triggerScenarios":"Executing INSERT INTO t JSON 'null' or binding a prepared-statement JSON string whose parsed Map is null (e.g. the string \"null\" was passed as jsonString).","commonSituations":"Application code passing an uninitialized/serialized-null value into a JSON INSERT; API payloads where the body was 'null' and forwarded verbatim; template-generated queries with a missing object.","solutions":["Ensure the JSON argument is an object like '{\"col\": \"value\"}', never 'null'","Check the application variable feeding the JSON string for null/\"null\" values before executing","Guard at the API layer: reject empty or null JSON bodies before issuing INSERT JSON","Use fromJsonObject/toJson round-trips in tests to validate the payload"],"exampleFix":"// before\nString json = null; // or \"null\"\nsession.execute(\"INSERT INTO t JSON ?\", json);\n// after\nif (json == null || json.equals(\"null\")) throw new IllegalArgumentException(\"JSON payload required\");\nsession.execute(\"INSERT INTO t JSON ?\", json);","handlingStrategy":"validation","validationCode":"void requireJsonObject(String json) {\n    if (json == null || json.trim().isEmpty() || \"null\".equals(json.trim()))\n        throw new IllegalArgumentException(\"INSERT JSON payload must be a JSON object, got: \" + json);\n    if (json.trim().charAt(0) != '{')\n        throw new IllegalArgumentException(\"INSERT JSON payload must start with '{'\");\n}","typeGuard":null,"tryCatchPattern":"try { session.execute(\"INSERT INTO t JSON ?\", json); } catch (InvalidRequestException e) { if (e.getMessage().contains(\"Got null\")) throw new IllegalArgumentException(\"Null JSON payload\", e); throw e; }","preventionTips":["Never bind literal 'null' as a JSON document","Validate payloads are JSON objects at the API boundary","Use a JSON library to serialize maps rather than templates","Add integration tests for empty/null payload cases"],"tags":["cql","json","insert"],"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"}