{"record":{"id":"63e7cebc774ae4b4","repo":"karatelabs/karate","slug":"unexpected-end-of-json-input-in-array","errorCode":null,"errorMessage":"Unexpected end of JSON input in array","messagePattern":"Unexpected end of JSON input in array","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsonParser.java","lineNumber":161,"sourceCode":"                throw syntaxError(\"Expected ',' or '}' in object\");\n            }\n        }\n\n        private List<Object> parseArray() {\n            // we know s.charAt(pos) == '['\n            pos++;\n            List<Object> list = new ArrayList<>();\n            skipWs();\n            if (pos < len && s.charAt(pos) == ']') {\n                pos++;\n                return list;\n            }\n            while (true) {\n                skipWs();\n                list.add(parseValue());\n                skipWs();\n                if (pos >= len) {\n                    throw syntaxError(\"Unexpected end of JSON input in array\");\n                }\n                char c = s.charAt(pos);\n                if (c == ',') {\n                    pos++;\n                    // trailing-comma rejected: the next parseValue() lands on\n                    // ']' and parseValue's default branch throws.\n                    continue;\n                }\n                if (c == ']') {\n                    pos++;\n                    return list;\n                }\n                throw syntaxError(\"Expected ',' or ']' in array\");\n            }\n        }\n\n        private String parseString() {\n            // we know s.charAt(pos) == '\"'","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsonParser.java#L143-L179","documentation":"Thrown by JsonParser.parseArray when the parser reaches end of input while inside a JSON array, before finding the closing ']'. After parsing an element and skipping whitespace, the input ends, so the array is unterminated and the parse fails per RFC 8259.","triggerScenarios":"Parsing input like '[1,2' or '[\"a\",' — an array whose ']' is missing before the end of the string.","commonSituations":"Truncated API response bodies, JSON arrays cut off while streaming/reading a file, log capture that clipped the payload, dynamic array building that forgot the closing bracket.","solutions":["Inspect the raw string being parsed and append the missing ']' (and finish any half-added element).","Validate the payload with a JSON linter before parsing.","If read from a file or network, ensure the full content was read (check for truncation/stream limits).","Fix dynamic construction so the closing bracket is always emitted."],"exampleFix":"// before\nvar list = karate.fromJson('[1, 2, 3');\n// after\nvar list = karate.fromJson('[1, 2, 3]');","handlingStrategy":"validation","validationCode":"static boolean isCompleteArray(String json) {\n    String t = json == null ? \"\" : json.trim();\n    return t.startsWith(\"[\") && t.endsWith(\"]\") && t.length() > 1;\n}\n// usage\nif (!isCompleteArray(body)) throw new IllegalArgumentException(\"Truncated JSON array\");","typeGuard":"static boolean looksLikeJsonArray(String s) {\n    if (s == null) return false;\n    String t = s.trim();\n    return t.startsWith(\"[\") && t.endsWith(\"]\");\n}","tryCatchPattern":"try {\n    List<Object> list = Json.parseArray(raw);\n} catch (JsonSyntaxException e) {\n    log.error(\"Unterminated JSON array, raw=[{}]\", raw);\n    return Collections.emptyList(); // or rethrow with context\n}","preventionTips":["Check response completeness (stream fully read, content-length matched) before parsing.","Lint fixture files containing arrays in CI.","Build arrays with a serializer to guarantee brackets.","Beware of string truncation limits in log capture pipelines."],"tags":["json","syntax-error","parser"],"backgroundTag":"json-parse-error","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}