{"record":{"id":"4436db94c5ff3944","repo":"json-path/JsonPath","slug":"json-object-cannot-be-databind-to-targettype","errorCode":null,"errorMessage":"JSON object cannot be databind to \" + targetType","messagePattern":"JSON object cannot be databind to \" \\+ targetType","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java","lineNumber":196,"sourceCode":"                        JsonParser jsonParser = new JsonStructureToParserAdapter((JsonStructure) source);\r\n                        return jsonToTypeMethod.invoke(jsonb, jsonParser, (Type) targetType);\r\n                    } catch (Exception e){\r\n                        throw new MappingException(e);\r\n                    }\r\n                } else {\r\n                    try {\r\n                        // Fallback databinding approach for JSON-B API implementations without\r\n                        // explicit support for use of JsonParser in their public API. The approach\r\n                        // is essentially first to serialize given value into JSON, and then bind\r\n                        // the JSON string to data object of given type.\r\n                        String json = source.toString();\r\n                        return jsonb.fromJson(json, (Type) targetType);\r\n                    } catch (JsonbException e){\r\n                        throw new MappingException(e);\r\n                    }\r\n                }\r\n            } else {\r\n                throw new MappingException(\"JSON object cannot be databind to \" + targetType);\r\n            }\r\n        } else {\r\n            return source;\r\n        }\r\n    }\r\n\r\n    @SuppressWarnings(\"unchecked\")\r\n    private <T> T mapIntegralJsonNumber(JsonNumber jsonNumber, Class<?> targetType) {\r\n        if (targetType.isPrimitive()) {\r\n            if (int.class.equals(targetType)) {\r\n                return (T) Integer.valueOf(jsonNumber.intValueExact());\r\n            } else if (long.class.equals(targetType)) {\r\n                return (T) Long.valueOf(jsonNumber.longValueExact());\r\n            }\r\n        } else if (Integer.class.equals(targetType)) {\r\n            return (T) Integer.valueOf(jsonNumber.intValueExact());\r\n        } else if (Long.class.equals(targetType)) {\r\n            return (T) Long.valueOf(jsonNumber.longValueExact());\r","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java#L178-L214","documentation":"In mapImpl, when the source is a JsonObject and the target type is not Map/JsonObject and JSON-B databinding is unavailable (no Jsonb instance configured, or jsonb provider not on classpath), the provider cannot convert the object and throws MappingException. The JSON-B fallback path is the only way to bind a JsonObject to an arbitrary POJO in this provider.","triggerScenarios":"read(path, MyPojo.class) where the path resolves to a JSON object AND no Jsonb is configured/available in JakartaMappingProvider; or the targetType is a type jsonb.fromJson does not accept (the else branch).","commonSituations":"Using the Jakarta provider without a JSON-B implementation (e.g. Yasson) on the classpath, so POJO databinding silently isn't available; mapping to a POJO while only Map/JsonObject conversion is supported; expecting default-provider behavior (Jackson/Jayway databinding) after switching MappingConfiguration.","solutions":["Add a JSON-B implementation (e.g. org.eclipse:yasson) to the classpath so the jsonb.fromJson path works","Map to Map.class or JsonObject.class instead of a POJO and extract fields manually","Register/configure a Jsonb instance with the provider if it supports injection","Switch MappingConfiguration to a provider with built-in databinding (JacksonJsonProvider, GsonJsonProvider)"],"exampleFix":"// before\nUser u = ctx.read(\"$.user\", User.class); // no Jsonb -> MappingException\n// after\nMap<String, Object> u = ctx.read(\"$.user\", Map.class);\n// or add yasson dependency and keep User.class","handlingStrategy":"fallback","validationCode":"Configuration.setDefaults(new Configuration.Defaults() {\n    public MappingProvider mappingProvider() { return new JakartaMappingProvider(); }\n    // ensure a Jsonb-backed path exists or prefer Map.class targets\n});\n// pre-check: prefer Map/JsonObject for objects when no Jsonb on classpath\nObject raw = ctx.read(path);\nif (raw instanceof Map) { /* bind manually */ }","typeGuard":"boolean canDatabind(Class<?> t) { return Map.class.equals(t) || JsonObject.class.equals(t) /* or jsonb available */; }","tryCatchPattern":"try {\n    User u = ctx.read(path, User.class);\n} catch (MappingException e) {\n    Map<String, Object> m = ctx.read(path, Map.class);\n    // map to POJO manually\n}","preventionTips":["Ensure a JSON-B implementation (yasson) is on the classpath when using JakartaMappingProvider with POJOs","Prefer Map.class/JsonObject.class targets for JSON objects","Pin MappingConfiguration explicitly so team members don't unknowingly switch providers","Test typed reads in CI with the same provider configuration as production"],"tags":["json","databinding","json-path","jsonb"],"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"}