{"record":{"id":"63ac4ac1a84e28ca","repo":"json-path/JsonPath","slug":"cannot-create-json-iterator-for-value","errorCode":null,"errorMessage":"Cannot create JSON iterator for \" + value","messagePattern":"Cannot create JSON iterator for \" \\+ value","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java","lineNumber":545,"sourceCode":"                while (scope.hasNext()) {\r\n                    scope.next();\r\n                }\r\n                state = Event.END_OBJECT;\r\n            }\r\n        }\r\n\r\n        @Override\r\n        public void close() {\r\n            // JSON objects are read-only\r\n        }\r\n\r\n        private JsonStructureScope createScope(JsonValue value) {\r\n            if (value instanceof JsonArray) {\r\n                return new JsonArrayScope((JsonArray) value);\r\n            } else if (value instanceof JsonObject) {\r\n                return new JsonObjectScope((JsonObject) value);\r\n            }\r\n            throw new JsonException(\"Cannot create JSON iterator for \" + value);\r\n        }\r\n\r\n        private Event getState(JsonValue value) {\r\n            switch (value.getValueType()) {\r\n            case ARRAY:\r\n                return Event.START_ARRAY;\r\n            case OBJECT:\r\n                return Event.START_OBJECT;\r\n            case STRING:\r\n                return Event.VALUE_STRING;\r\n            case NUMBER:\r\n                return Event.VALUE_NUMBER;\r\n            case TRUE:\r\n                return Event.VALUE_TRUE;\r\n            case FALSE:\r\n                return Event.VALUE_FALSE;\r\n            case NULL:\r\n                return Event.VALUE_NULL;\r","sourceCodeStart":527,"sourceCodeEnd":563,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java#L527-L563","documentation":"createScope() in JakartaMappingProvider's JsonStructureToParserAdapter can only iterate JsonArray and JsonObject structures. If it is handed any other JsonValue (a scalar like JsonString, JsonNumber, JsonValue.TRUE/FALSE/NULL), it throws JsonException because no container iterator exists for scalars. This is called by the adapter constructor and by next() when advancing into nested values.","triggerScenarios":"Driving the adapter with a root or nested JsonValue that is a scalar (string, number, true, false, null) rather than a JsonObject or JsonArray — e.g. mapping the result of a JSON-P query that selected a single scalar.","commonSituations":"A JsonPath expression like $.name or $[0] that resolves to a scalar is fed to the Jakarta mapping provider, which expects a JSON structure at the root; upgrading providers so a previously object/array root is now scalar.","solutions":["Wrap or normalize scalars before mapping — ensure the value passed to map/toJson is a JsonObject or JsonArray","Refine the JsonPath so it returns a structure, or read scalar results directly instead of via the streaming adapter","Catch JsonException and handle scalar results as leaf values in your code"],"exampleFix":"// before\nObject doc = Json.createObjectBuilder().build();\nJsonValue scalar = jsonPath.read(doc); // may be a JsonString\nmapper.map(scalar, ...); // JsonException\n// after\nJsonValue scalar = jsonPath.read(doc);\nif (scalar instanceof JsonStructure) {\n    mapper.map(scalar, ...);\n} else {\n    Object v = unwrapScalar(scalar); // handle string/number/bool/null directly\n}","handlingStrategy":"validation","validationCode":"if (!(value instanceof JsonStructure)) {\n    throw new IllegalArgumentException(\"Expected JsonObject or JsonArray, got: \" + value.getValueType());\n}","typeGuard":"boolean isIterableStructure(JsonValue v) {\n    return v instanceof JsonArray || v instanceof JsonObject;\n}","tryCatchPattern":"try {\n    mapper.map(value, typeRef, config);\n} catch (JsonException e) {\n    // value was a scalar; unwrap or wrap it before mapping\n}","preventionTips":["Only feed JsonObject/JsonArray roots into the streaming adapter","Handle scalar JsonPath results (strings, numbers, booleans, null) as leaf values outside the adapter","Write unit tests covering both container and scalar read results"],"tags":["json","json-path","json-p","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"}