{"record":{"id":"29036ed0690e3a2b","repo":"json-path/JsonPath","slug":"invalid-json","errorCode":null,"errorMessage":"Invalid JSON","messagePattern":"Invalid JSON","errorType":"exception","errorClass":"JSONException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/json/JettisonProvider.java","lineNumber":202,"sourceCode":"\t\t}\n\t\t\n\t}\n\t\n\tprivate Object parse(JettisonTokener JsonTokener)\n\t{\n\t\ttry\n\t\t{\n\t\t\tchar nextChar = JsonTokener.nextClean();\n\t\t\tJsonTokener.back();\n\t\t\tif (nextChar == '{') \n\t\t\t{\n\t\t\t\treturn new JettisonObject(JsonTokener);\n\t\t\t}\n\t\t\tif (nextChar == '[') \n\t\t\t{\n\t\t\t\treturn new JettisonArray(JsonTokener);\n\t\t\t}\n\t\t\tthrow new JSONException(\"Invalid JSON\");\n\t\t}\n\t\tcatch( org.codehaus.jettison.json.JSONException jsonException )\n\t\t{\n\t\t\tthrow new IllegalStateException(jsonException);\n\t\t}\n\t}\n\t\n\t@Override\n\tpublic Object parse(String json) throws InvalidJsonException \n\t{\n\t\treturn parse(new JettisonTokener(json));\n\t}\n\t\n\t@Override\n\tpublic Object parse(InputStream jsonStream, String charset) throws InvalidJsonException\n\t{\n\t\ttry\n\t\t{","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/json/JettisonProvider.java#L184-L220","documentation":"JettisonProvider.parse(JettisonTokener) peeks at the first non-whitespace character of the input and only accepts '{' (object) or '[' (array). Any other starting character — a bare string, number, 'true'/'false'/'null', or garbage — throws JSONException('Invalid JSON'). Note that jettison JSONException here is not caught by the enclosing catch of org.codehaus.jettison.json.JSONException unless it is that same class, in which case it becomes IllegalStateException; callers of parse(String) are declared to receive InvalidJsonException.","triggerScenarios":"Calling JsonPath.parse(json) on a document that starts with anything other than '{' or '[' — e.g. a top-level scalar like '\"hello\"' or '42', an empty/blank string, HTML error pages, or truncated JSON whose first char is a stray token.","commonSituations":"APIs returning plain-text error pages (HTML '<html>...') with HTTP 200; services that return bare scalars which JSON-P-based providers accept but Jettison does not; empty response bodies from failed upstream calls; BOM or whitespace-only payloads where nextClean yields an unexpected char.","solutions":["Validate the payload starts with '{' or '[' before parsing: trim and check the first character, or attempt JSONValue validation","If the API can return bare scalars, wrap them before parsing (e.g. serialize into an object) or use a different provider (JakartaJsonProvider/Jackson) that accepts scalar roots","Catch InvalidJsonException (and IllegalStateException wrapping jettison JSONException) around JsonPath.parse and surface the raw payload for debugging","Check HTTP response content-type and status before parsing to reject HTML/empty error bodies early"],"exampleFix":"// before\nDocumentContext ctx = JsonPath.parse(responseBody); // body may be \"42\" or HTML\n// after\nString trimmed = responseBody == null ? \"\" : responseBody.trim();\nif (trimmed.startsWith(\"{\") || trimmed.startsWith(\"[\")) {\n    DocumentContext ctx = JsonPath.parse(trimmed);\n} else {\n    throw new InvalidJsonException(\"Expected JSON object/array, got: \" + trimmed);\n}","handlingStrategy":"validation","validationCode":"// Java\nstatic void requireJsonContainer(String body) {\n    String t = body == null ? \"\" : body.trim();\n    if (!(t.startsWith(\"{\") || t.startsWith(\"[\")))\n        throw new InvalidJsonException(\"Not a JSON object/array: \" + t.substring(0, Math.min(50, t.length())));\n}","typeGuard":"boolean looksLikeJson(String body) {\n    if (body == null) return false;\n    String t = body.trim();\n    return t.startsWith(\"{\") || t.startsWith(\"[\");\n}","tryCatchPattern":"try {\n    DocumentContext ctx = JsonPath.parse(body);\n} catch (InvalidJsonException | IllegalStateException e) {\n    // log raw payload, check content-type / HTTP status\n}","preventionTips":["Verify HTTP content-type is application/json before parsing","Reject empty or HTML bodies before handing them to JsonPath.parse","Strip UTF-8 BOM from response streams","If top-level scalars are expected, pre-wrap them or use a provider that accepts scalar roots"],"tags":["json","jettison","parse-error","json-path"],"backgroundTag":"json-parse-error","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"}