java data_error ai_generated true

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

ID: java/jackson-jsonparseexception-unexpected-char

Also available as: JSON · Markdown · 中文
85%Fix Rate
88%Confidence
1Evidence
2024-04-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Jackson 2.13 active
Jackson 2.14 active
Jackson 2.15 active
Jackson 2.16 active

Root Cause

Jackson is trying to parse a response or input that contains non-JSON content, typically an HTML page (starting with '<') instead of valid JSON, often due to an incorrect URL, server error response, or missing API endpoint.

generic

中文

Jackson 尝试解析包含非 JSON 内容的响应或输入,通常是一个 HTML 页面(以 '<' 开头)而不是有效的 JSON,通常是由于错误的 URL、服务器错误响应或缺少 API 端点。

Official Documentation

https://fasterxml.github.io/jackson-core/javadoc/2.14/com/fasterxml/jackson/core/JsonParseException.html

Workarounds

  1. 85% success Check the actual response from the server by logging the raw response string before parsing. For example: System.out.println("Response: " + responseBody); and verify the URL or API endpoint.
    Check the actual response from the server by logging the raw response string before parsing. For example: System.out.println("Response: " + responseBody); and verify the URL or API endpoint.
  2. 90% success Use a debug proxy like Fiddler or Charles Proxy to capture the HTTP response and confirm it's HTML instead of JSON. Then fix the request URL or handle HTTP error codes (e.g., 404, 500) gracefully.
    Use a debug proxy like Fiddler or Charles Proxy to capture the HTTP response and confirm it's HTML instead of JSON. Then fix the request URL or handle HTTP error codes (e.g., 404, 500) gracefully.
  3. 80% success Add a response content-type check before parsing: if (response.getHeader("Content-Type").contains("application/json")) { parse; } else { handle error; }
    Add a response content-type check before parsing: if (response.getHeader("Content-Type").contains("application/json")) { parse; } else { handle error; }

中文步骤

  1. Check the actual response from the server by logging the raw response string before parsing. For example: System.out.println("Response: " + responseBody); and verify the URL or API endpoint.
  2. Use a debug proxy like Fiddler or Charles Proxy to capture the HTTP response and confirm it's HTML instead of JSON. Then fix the request URL or handle HTTP error codes (e.g., 404, 500) gracefully.
  3. Add a response content-type check before parsing: if (response.getHeader("Content-Type").contains("application/json")) { parse; } else { handle error; }

Dead Ends

Common approaches that don't work:

  1. Add @JsonIgnoreProperties(ignoreUnknown = true) to the POJO class 95% fail

    This annotation ignores unknown JSON properties but does not help when the input is not JSON at all (e.g., HTML).

  2. Increase the Jackson parsing leniency with ObjectMapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES) 90% fail

    These features relax JSON syntax rules but still require valid JSON structure; they cannot parse HTML.

  3. Wrap the parsing in a try-catch and return an empty object 80% fail

    This hides the error but does not address the root cause (the server returning HTML). The application may behave incorrectly.