org.codehaus.jackson.JsonParseException: Unexpected character ('�' (code 65533 / 0xfffd))

31k views Asked by At

I have a Json string in the database but while converting in Java object, it gives following error:

Caused by: org.codehaus.jackson.JsonParseException: Unexpected character ('�' (code 65533 / 0xfffd)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')

Json is : {"crt":"wrd","name":"7|6A TTTM"}

In java code I have configured it and have made it private (not static final)

objectMapper= new ObjectMapper();
objectMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

Note: It some time converts that Json string in Object but some time gives above error. Why this unexpected result comes?

3

There are 3 answers

0
Priyamal On

this worked for me.

String formattedString = yourString.trim().replaceAll("\uFFFD", "");
1
StaxMan On

Something is producing invalid UTF-8 sequence (or, mismatch of UTF-8 vs a single-byte encoding like ISO-8859-1), and Jackson detects this encoding problem. It has nothing to do with ACCEPT_SINGLE_VALUE_AS_ARRAY setting, as the exception comes from low-level JsonParser.

So you need to figure out why the JSON content to parse is corrupt.

2
Mihnea On

Short answer solution: Remove the first occurrence of the extra added BOM text with a method, such as the following, should fix this issue:

public String cleanUpJsonBOM(String json) {
      return json.trim().replaceFirst("\ufeff", "");
  }

I had a similar issue which I documented in a blog post. Hope this help!