{"record":{"id":"467586f21b34ecf7","repo":"json-path/JsonPath","slug":"invalidjsonexception-467586","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/JacksonJsonNodeJsonProvider.java","lineNumber":54,"sourceCode":"    public JacksonJsonNodeJsonProvider() {\n        this(defaultObjectMapper);\n    }\n\n    /**\n     * Initialize the JacksonJsonNodeJsonProvider with a custom ObjectMapper and ObjectReader.\n     *\n     * @param objectMapper the ObjectMapper to use\n     */\n    public JacksonJsonNodeJsonProvider(ObjectMapper objectMapper) {\n        this.objectMapper = objectMapper;\n    }\n\n    @Override\n    public Object parse(String json) throws InvalidJsonException {\n        try {\n            return objectMapper.readTree(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 objectMapper.readTree(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 objectMapper.readTree(new InputStreamReader(jsonStream, charset));\n        } catch (IOException e) {","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java#L36-L72","documentation":"JacksonJsonNodeJsonProvider.parse(String) wraps IOException from objectMapper.readTree into InvalidJsonException. The input string could not be parsed into a Jackson JsonNode tree — it is not valid JSON, empty, or contains a BOM/trailing garbage.","triggerScenarios":"JsonPath.parse(String) configured with the JacksonJsonNodeJsonProvider when the string is malformed JSON, empty/whitespace-only, an HTML error page, or a JSON5/comments document that Jackson's strict mode rejects.","commonSituations":"Downstream APIs returning HTML on auth expiry; copy-pasted JSON with trailing commas or comments; reading response bodies of failed requests without checking status; empty string from an empty file.","solutions":["Print/inspect the offending string (it is included as the exception's detail) and validate with a JSON parser first","Check response status/content-type before parsing; route non-JSON bodies to error handling","Remove BOM, trailing commas, or comments, or enable Jackson's allowed-non-numeric-numbers/allow-comments features if appropriate","Verify Configuration is intentionally using JacksonJsonNodeJsonProvider (it returns JsonNode, not Map/List) and not a misconfiguration"],"exampleFix":"// before\nDocumentContext ctx = JsonPath.parse(rawBody);\n// after\nif (rawBody == null || rawBody.isBlank()) throw new EmptyBodyException();\nDocumentContext ctx = JsonPath.parse(rawBody);","handlingStrategy":"validation","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 isJsonString(String s) {\n    if (s == null) return false;\n    String t = s.trim();\n    return (t.startsWith(\"{\") && t.endsWith(\"}\")) || (t.startsWith(\"[\") && t.endsWith(\"]\"));\n}","tryCatchPattern":"try {\n    DocumentContext ctx = JsonPath.parse(jsonString);\n} catch (InvalidJsonException e) {\n    log.error(\"Invalid JSON input: {}\", e.getMessage());\n    throw new BadRequestException(\"Input is not valid JSON\");\n}","preventionTips":["Check response status/content-type before treating a body as JSON","Trim and BOM-strip strings from files and editors","Keep a single shared Configuration so the intended provider is always used","Validate sample payloads with a JSON linter in CI"],"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"}