user2334926
user2334926

Reputation: 341

com.fasterxml.jackson.core.JsonParseException: Unexpected character(code 160) while reading the json file

I am reading the below json content from a file and converting into a map but i am getting the below exception. Kindly let me know if anybody has come across such issue. I validated my json content and looks valid. Not sure why this error.

Json Content:

{
    "Results":[{         
        "TotalPositiveFeedbackCount": 0      
    },{
        "TotalPositiveFeedbackCount": 1      
    }   ]
}

Code:

Map<String, Object> domainMap = new HashMap<String, Object>();
try {
    responseJson = getFile("reviewresponse.json");
    //responseJson = new String(Files.readAllBytes(Paths.get("reviewresponse.json")), StandardCharsets.UTF_8);
    ObjectMapper jsonObjectMapper = new ObjectMapper();
    jsonObjectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    domainMap = jsonObjectMapper.readValue(responseJson,
                   new TypeReference<Map<String, Object>>() {});
} 

Exception Details:

com.fasterxml.jackson.core.JsonParseException: Unexpected character (' ' (code 160)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name
 at [Source: {
    "Results":[{         
        "TotalPositiveFeedbackCount": 0      
    },{
        "TotalPositiveFeedbackCount": 1      
    }   ]
}
; line: 2, column: 15]

Upvotes: 7

Views: 41501

Answers (3)

Rajeeva Gowda
Rajeeva Gowda

Reputation: 21

had same issue with body sent via postman Just copy pasted the text into sublime text editor which shown as below, The picture shows some extra formatting in space which is not a valid json white space

After removing them and just spacing properly in sublime worked well.

Upvotes: 1

Dhiraj Parakh
Dhiraj Parakh

Reputation: 11

Google for Json formatter then click on any one of the option , Paste your json code in that formatter , it will validate & highlight the unwanted characters just delete them or replace then with what you want.

e.g. goto https://jsonformatter.org/ & follow the above steps

Upvotes: 0

dustin.schultz
dustin.schultz

Reputation: 13676

Your JSON content contains non-breaking spaces (character code 160, commonly known as &nbsp;) likely from copying and pasting JSON (usually from a webpage) that used &nbsp; to indent the JSON.

enter image description here

You can fix it with

responseJson = responseJson.replace('\u00A0',' ');

Upvotes: 13

Related Questions