{"record":{"id":"d04202d385464008","repo":"json-path/JsonPath","slug":"cannot-map-json-element-to-typename","errorCode":null,"errorMessage":"Cannot map JSON element to \" + typeName","messagePattern":"Cannot map JSON element to \" \\+ typeName","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java","lineNumber":377,"sourceCode":"                if (Modifier.isPublic(mods) && !Modifier.isAbstract(mods) &&\r\n                        name.equals(method.getName()) &&\r\n                        Arrays.equals(paramTypes, method.getParameterTypes())) {\r\n                    return method;\r\n                }\r\n            }\r\n            clazz = clazz.getSuperclass();\r\n        }\r\n        return null;\r\n    }\r\n\r\n    private Class<?> getRawClass(Type targetType) {\r\n        if (targetType instanceof Class) {\r\n            return (Class<?>) targetType;\r\n        } else if (targetType instanceof ParameterizedType) {\r\n            return (Class<?>) ((ParameterizedType) targetType).getRawType();\r\n        } else if (targetType instanceof GenericArrayType) {\r\n            String typeName = targetType.getTypeName();\r\n            throw new MappingException(\"Cannot map JSON element to \" + typeName);\r\n        } else {\r\n            String typeName = targetType.getTypeName();\r\n            throw new IllegalArgumentException(\"TypeRef not supported: \" + typeName);\r\n        }\r\n    }\r\n\r\n    private Type getFirstTypeArgument(Type targetType) {\r\n        if (targetType instanceof ParameterizedType) {\r\n            Type[] args = ((ParameterizedType) targetType).getActualTypeArguments();\r\n            if (args != null && args.length > 0) {\r\n                if (args[0] instanceof Class) {\r\n                    return (Class<?>) args[0];\r\n                } else if (args[0] instanceof ParameterizedType) {\r\n                    return (ParameterizedType) args[0];\r\n                }\r\n            }\r\n        }\r\n        return null;\r","sourceCodeStart":359,"sourceCodeEnd":395,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java#L359-L395","documentation":"getRawClass() reduces a java.lang.reflect Type to its raw Class for databinding. Class and ParameterizedType (e.g. List<Book>) are handled, but a GenericArrayType — a type like T[] or Book[] obtained through generics/TypeRef — has no single raw class the mapper can target, so it throws MappingException \"Cannot map JSON element to <typeName>\". The provider does not support generic array databinding.","triggerScenarios":"read(path, new TypeRef<Book[]>(){}) or any TypeRef/field generic resolving to an array type (G[]), where G is itself generic, so the Type is a GenericArrayTypeImpl rather than a plain Class.","commonSituations":"Using TypeRef with array syntax instead of List; mapping generic methods whose return Type is T[]; migrating code from Jackson-backed providers (which support arrays) to the Jakarta/JSON-P provider which does not.","solutions":["Replace array target types with collections: new TypeRef<List<Book>>(){} instead of new TypeRef<Book[]>(){}.","If the array type is a plain concrete class (e.g. int[].class is a Class), verify what Type you actually pass — only GenericArrayType triggers this; restructure generics so a concrete Class is resolved.","Read as List<Object> and convert with list.toArray(new Book[0]) manually.","Catch MappingException and fall back to the List-based mapping."],"exampleFix":"// before\nList<Book> books = java.util.Arrays.asList(jsonPath.read(\"$.store.book\", new TypeRef<Book[]>() {})); // MappingException\n// after\nList<Book> books = jsonPath.read(\"$.store.book\", new TypeRef<List<Book>>() {});","handlingStrategy":"type-guard","validationCode":"static boolean isArrayType(java.lang.reflect.Type t) {\n    return t instanceof GenericArrayType\n        || (t instanceof Class && ((Class<?>) t).isArray());\n}","typeGuard":"boolean safeTarget(java.lang.reflect.Type t) {\n    return t instanceof Class || t instanceof ParameterizedType;\n}","tryCatchPattern":"try {\n    return jsonPath.read(path, typeRef);\n} catch (MappingException e) {\n    return jsonPath.read(path, new TypeRef<List<T>>() {});\n}","preventionTips":["Never use array targets (T[], Book[]) with TypeRef on this provider; use List<T>.","Avoid generic arrays in APIs that feed Types into JsonPath reads.","Convert List results to arrays yourself after mapping."],"tags":["json","generics","type-mismatch","json-path"],"backgroundTag":"unsupported-operation","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"}