{"record":{"id":"0be1fcba7499b424","repo":"json-path/JsonPath","slug":"json-boolean-false-cannot-be-mapped-to-class","errorCode":null,"errorMessage":"JSON boolean (false) cannot be mapped to \" + className","messagePattern":"JSON boolean \\(false\\) cannot be mapped to \" \\+ className","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java","lineNumber":115,"sourceCode":"\r\n    private Object mapImpl(Object source, final Type targetType) {\r\n        if (source == null || source == JsonValue.NULL) {\r\n            return null;\r\n        }\r\n        if (source == JsonValue.TRUE) {\r\n            if (Boolean.class.equals(targetType)) {\r\n                return Boolean.TRUE;\r\n            } else {\r\n                String className = targetType.toString();\r\n                throw new MappingException(\"JSON boolean (true) cannot be mapped to \" + className);\r\n            }\r\n        }\r\n        if (source == JsonValue.FALSE) {\r\n            if (Boolean.class.equals(targetType)) {\r\n                return Boolean.FALSE;\r\n            } else {\r\n                String className = targetType.toString();\r\n                throw new MappingException(\"JSON boolean (false) cannot be mapped to \" + className);\r\n            }\r\n        } else if (source instanceof JsonString) {\r\n            if (String.class.equals(targetType)) {\r\n                return ((JsonString) source).getChars();\r\n            } else {\r\n                String className = targetType.toString();\r\n                throw new MappingException(\"JSON string cannot be mapped to \" + className);\r\n            }\r\n        } else if (source instanceof JsonNumber) {\r\n            JsonNumber jsonNumber = (JsonNumber) source;\r\n            if (jsonNumber.isIntegral()) {\r\n                return mapIntegralJsonNumber(jsonNumber, getRawClass(targetType));\r\n            } else {\r\n                return mapDecimalJsonNumber(jsonNumber, getRawClass(targetType));\r\n            }\r\n        }\r\n        if (source instanceof JsonArrayBuilder) {\r\n            source = ((JsonArrayBuilder) source).build();\r","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java#L97-L133","documentation":"JakartaMappingProvider.mapImpl converts a JSON-P JsonValue into a Java type. When the source is JsonValue.FALSE and the requested targetType is anything other than Boolean (or boolean via earlier checks), it cannot convert and throws MappingException. This is a deliberate strictness: the provider does not coerce booleans to strings, numbers, or objects.","triggerScenarios":"Calling JsonPath.parse(json).read(path, Class) (or .map(...) / mapping via the provider) where the JSON value at the path is literally false and the requested target type is not Boolean.class/boolean — e.g. read(\"$.enabled\", String.class) or read(\"$.flag\", Integer.class).","commonSituations":"Path points at a boolean field while a String/numeric type was assumed (API schema changed true/false into an enum-like field); config where a flag is expected to be a string \"false\" but the document contains real JSON false; switching from the default JsonSmartMappingProvider (looser coercions) to JakartaMappingProvider and old coercions now fail.","solutions":["Change the requested target type to Boolean.class (or boolean) so the false value maps directly","Fix the JSON path to select a value of the expected type, or verify the document shape at the path","Use .map(...) with a custom conversion lambda instead of direct type mapping","Configure a different MappingProvider (e.g. JsonSmartMappingProvider) if lenient coercion is desired"],"exampleFix":"// before\nString enabled = context.read(\"$.enabled\", String.class); // MappingException\n// after\nBoolean enabled = context.read(\"$.enabled\", Boolean.class);","handlingStrategy":"type-guard","validationCode":"Object raw = ctx.read(path);\nif (!(raw instanceof Boolean)) throw new IllegalStateException(path + \" is not a JSON boolean: \" + raw);\nBoolean b = ctx.read(path, Boolean.class);","typeGuard":"boolean isBoolean(Object v) { return v instanceof Boolean || v == Boolean.FALSE || v == Boolean.TRUE; }","tryCatchPattern":"try {\n    Boolean b = ctx.read(path, Boolean.class);\n} catch (MappingException e) {\n    // fall back to raw read and convert\n    Object raw = ctx.read(path);\n}","preventionTips":["Always request Boolean.class/boolean for boolean JSON values","Validate document shape against a JSON schema before reading","Check the value with a raw read (ctx.read(path)) before typed mapping","Remember JakartaMappingProvider does not coerce across types"],"tags":["json","type-mapping","json-path","jakarta-json"],"backgroundTag":"incompatible-source-type","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}