{"record":{"id":"ee966ef09cd60927","repo":"json-path/JsonPath","slug":"invalidjsonexception-ee966e","errorCode":null,"errorMessage":"InvalidJsonException","messagePattern":"InvalidJsonException","errorType":"exception","errorClass":"InvalidJsonException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonProvider.java","lineNumber":73,"sourceCode":"      this(objectMapper, objectMapper.reader().forType(Object.class));\n    }\n\n    /**\n     * Initialize the JacksonProvider with a custom ObjectMapper and ObjectReader.\n     * @param objectMapper the ObjectMapper to use\n     * @param objectReader the ObjectReader to use\n     */\n    public JacksonJsonProvider(ObjectMapper objectMapper, ObjectReader objectReader) {\n      this.objectMapper = objectMapper;\n      this.objectReader = objectReader;\n    }\n\n    @Override\n    public Object parse(String json) throws InvalidJsonException {\n        try {\n            return objectReader.readValue(json);\n        } catch (IOException e) {\n            throw new InvalidJsonException(e, json);\n        }\n    }\n\n    @Override\n    public Object parse(byte[] json)\n        throws InvalidJsonException {\n        try {\n            return objectReader.readValue(json);\n        } catch (IOException e) {\n            throw new InvalidJsonException(e, new String(json, StandardCharsets.UTF_8));\n        }\n    }\n\n    @Override\n    public Object parse(InputStream jsonStream, String charset) throws InvalidJsonException {\n        try {\n            return objectReader.readValue(new InputStreamReader(jsonStream, charset));\n        } catch (IOException e) {","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonProvider.java#L55-L91","documentation":"JacksonJsonProvider.parse(String) wraps IOException from objectReader.readValue into InvalidJsonException. The string is not valid JSON for Jackson's reader — malformed syntax, wrong type shape (e.g. an array where a map is expected), empty input, or trailing content.","triggerScenarios":"JsonPath.parse(String) with the JacksonJsonProvider given malformed JSON, an HTML error page, JSON with comments/trailing commas in strict mode, or input whose root type conflicts with the expected mapping.","commonSituations":"APIs returning error pages on 401/500; hand-edited config JSON with syntax mistakes; provider differences after migrating between Gson/Jackson/Jackson3 providers; empty strings from failed file reads.","solutions":["Inspect the offending string (kept in the exception detail) and run it through a JSON validator","Check the HTTP status/content-type and handle non-JSON responses before parsing","Relax or fix strict-mode issues (allow comments/trailing commas) only if the producer legitimately emits them","Verify the deserialization target/config; JacksonJsonProvider returns Map/List, ensure the caller expects that shape"],"exampleFix":"// before\nDocumentContext ctx = JsonPath.parse(untrustedBody);\n// after\nif (!untrustedBody.trim().startsWith(\"{\")) {\n    throw new IllegalStateException(\"Expected JSON object, got: \" + untrustedBody);\n}\nDocumentContext ctx = JsonPath.parse(untrustedBody);","handlingStrategy":"try-catch","validationCode":"static boolean isValidJson(String s) {\n    if (s == null || s.isBlank()) return false;\n    try { new ObjectMapper().readTree(s); return true; } catch (IOException e) { return false; }\n}","typeGuard":"static boolean isJsonText(String s) {\n    if (s == null) return false;\n    String t = s.trim();\n    return t.startsWith(\"{\") || t.startsWith(\"[\");\n}","tryCatchPattern":"try {\n    DocumentContext ctx = JsonPath.parse(jsonString);\n} catch (InvalidJsonException e) {\n    log.error(\"JSON parse error: {} | input={}\", e.getCause(), e.getMessage());\n    throw new BadRequestException(\"Malformed JSON\");\n}","preventionTips":["Handle non-2xx responses before parsing the body","Detect HTML responses (starts with '<') explicitly","Validate JSON fixtures and configs in CI with a parser","Keep provider choice consistent across the application"],"tags":["json","jackson","parse","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"}