{"record":{"id":"c763e9d84bddd738","repo":"json-path/JsonPath","slug":"json-boolean-true-cannot-be-mapped-to","errorCode":null,"errorMessage":"JSON boolean (true) cannot be mapped to ","messagePattern":"JSON boolean \\(true\\) cannot be mapped to ","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java","lineNumber":107,"sourceCode":"     * a parameterized generic type is used.\r\n     */\r\n    @Override\r\n    public <T> T map(Object source, final TypeRef<T> targetType, Configuration configuration) {\r\n        @SuppressWarnings(\"unchecked\")\r\n        T result = (T) mapImpl(source, targetType.getType());\r\n        return result;\r\n    }\r\n\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","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java#L89-L125","documentation":"JakartaMappingProvider.mapImpl maps jakarta.json JsonValue sources to Java target types. When the source is JsonValue.TRUE (or FALSE) it can only return Boolean; if the requested targetType is anything else it throws MappingException naming the target class. JSON booleans cannot legally become e.g. String or Integer, so the mapping is refused.","triggerScenarios":"Calling JsonPath read with a target type like String.class or Integer.class on a path that resolves to a JSON true/false (JsonValue.TRUE/FALSE), e.g. read(\"$.enabled\", String.class) where the document has {\"enabled\": true}.","commonSituations":"Assuming booleans stringify to \"true\"; copying read calls with wrong target classes after a document schema change; generic read-into-map code requesting a fixed type for heterogeneous nodes.","solutions":["Read the boolean as Boolean.class and convert/represent as needed (String.valueOf) in your code","Validate the document schema so boolean nodes are not requested as other types","Use a mapping target of Object.class when the node type is unknown, then branch on the result","If the path may vary, guard with SUPPRESS_EXCEPTIONS/optional reads and handle type at the call site"],"exampleFix":"// before\nString s = JsonPath.parse(doc).read(\"$.enabled\", String.class); // throws\n// after\nBoolean b = JsonPath.parse(doc).read(\"$.enabled\", Boolean.class);\nString s = String.valueOf(b); // \"true\" / \"false\"\n","handlingStrategy":"try-catch","validationCode":"JsonValue v = doc.read(path);\nif (v == JsonValue.TRUE || v == JsonValue.FALSE) {\n    // request Boolean.class only\n}","typeGuard":"static boolean isJsonBoolean(jakarta.json.JsonValue v) {\n    return v == JsonValue.TRUE || v == JsonValue.FALSE;\n}","tryCatchPattern":"try {\n    T value = JsonPath.parse(doc).read(path, targetType);\n} catch (MappingException e) {\n    // re-read as Boolean and convert\n    Boolean b = JsonPath.parse(doc).read(path, Boolean.class);\n}","preventionTips":["Match the target class to the JSON type: Booleans read as Boolean.class","Read unknown-typed nodes as Object.class and branch at runtime","Keep document schema/tests aligned with the target classes used in reads"],"tags":["json","jsonpath","mapping","type-mismatch"],"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"}