{"record":{"id":"9dd357e49c1c3867","repo":"json-path/JsonPath","slug":"json-string-cannot-be-mapped-to-classname","errorCode":null,"errorMessage":"JSON string cannot be mapped to \" + className","messagePattern":"JSON string 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":122,"sourceCode":"                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\n        } else if (source instanceof JsonObjectBuilder) {\r\n            source = ((JsonObjectBuilder) source).build();\r\n        }\r\n        if (source instanceof Collection) {\r\n            // this covers both List<JsonValue> and JsonArray from JSON-P spec\r\n            Class<?> rawTargetType = getRawClass(targetType);\r\n            Type targetTypeArg = getFirstTypeArgument(targetType);\r","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java#L104-L140","documentation":"JakartaMappingProvider.mapImpl throws this when the source JsonValue is a JsonString but the requested targetType is not String (and not char sequence types handled elsewhere). The provider performs no automatic string coercion, so a JSON string cannot be mapped to e.g. Integer or Boolean.","triggerScenarios":"Calling read(path, SomeClass) where the JSON value at the path is a quoted string but the target class is not String — e.g. read(\"$.port\", Integer.class) when the document contains \"8080\".","commonSituations":"Documents where numeric-looking values are quoted (IDs, ports); expecting automatic parse of \"true\"/\"false\" or \"123\" into primitives; migrating from a lenient mapping provider that coerced strings to numbers/booleans.","solutions":["Request String.class and parse manually (Integer.parseInt, Boolean.parseBoolean)","Correct the JSON path so it selects a non-string value of the expected type","Use .map(...) with a custom converter that parses the string","Switch to a lenient MappingProvider if implicit coercion is acceptable"],"exampleFix":"// before\nint port = context.read(\"$.port\", int.class); // \"8080\" -> MappingException\n// after\nint port = Integer.parseInt(context.read(\"$.port\", String.class));","handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"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"}