Fix – Unexpected character (‘h’ (code 104))

Are you seeing the ERROR “Unexpected character (‘h’ (code 104))”, While trying to query a hive table stored as JSON files or while accessing the JSON file directly or using REST API to access a JSON file

org.apache.hadoop.hive.serde2.SerDeException: org.codehaus.jackson.JsonParseException: Unexpected character (‘h’ (code 104)): expected a valid value

Cause of the ERROR “Unexpected character (‘h’ (code 104))”

The error message “JsonParseException: Unexpected character (‘h’ (code 104))” indicates that there is an issue with the JSON data you are trying to parse. JSON file format has to be in specific syntax rules that must be followed

In this case, the error is pointing to the character ‘h’ at position 104 in the JSON data, suggesting that there is an unexpected character at that point, which is not valid JSON syntax.

JSON Structure:

JSON stands for JavaScript Object Notation, it stores the value as key-value pairs and the complete object will be surrounded by curly braces {}. below is an example of the JSON structure

{

"name":"Juno", 

    "age":32

}

Scenario 1:

In one of the cases, We had a Hive table on top of the JSON file. where it is failing while running a select query on top of the table (It can be any case either you are accessing the file directly or via a tool)

org.apache.hadoop.hive.serde2.SerDeException: org.codehaus.jackson.JsonParseException: Unexpected character ('h' (code 104)): expected a valid value 

We validated the JSON file and were able to find an invalid character, We can either re-populate the file or remove the char

Scenario 2:

In another case, if you are using the Jackson library to parse JSON files,You might have a high chance of getting this error

url = https://newsapi.org/v2/everything
return objectMapper.readValue(url, claxz);

In the above case, you will face this issue

Caused by: org.codehaus.jackson.JsonParseException: Unexpected character ('h' (code 104)): expected a valid value

In this case, the URL is considered a JSON string and getting parsed by Jackson library, Which causing the issue. you can make it work by making the URL as URL type


url = https://newsapi.org/v2/everything
return objectMapper.readValue(new URL(url), claxz);

General troubleshooting Steps:

Check the JSON Data: Check the JSON data for any syntax errors or unexpected characters. Pay attention to the character at the position and the surrounding context (As we discussed in the above section “(‘h’ (code 104))” – this means 104th position)

Verify Quotes and Commas: Make sure that all strings are enclosed in double quotes, and that each key-value pair is separated by a comma(except the last key-value pair, Which didn’t need a comma – example below).

JSON syntax is sensitive to the correct placement of quotes and commas. One little extra comma at the end will cause a syntax error as mentioned below

Verify Quotes and Commas:

Escape Special Characters: If you have special characters within strings, make sure to escape them properly using backslashes (\). Common special characters include double quotes ("), backslashes (\), and control characters.

Use a JSON Validator: You can use online JSON validators or parsers to validate your JSON data. These tools can highlight syntax errors and help you identify and fix issues. One example is JSONLint (https://jsonlint.com/).

Use a JSON Validator

Check for Hidden Characters: There are scenarios, Where some invisible or non-printable characters in your JSON data are causing the issue. Use a text editor or other tools that can display hidden characters or special symbols to identify and remove any unwanted characters.

For example in Linux, you can use “cat -v” to check the hidden chars

Review the Code That Generates JSON: If you are using any tool or custom code to generate JSON data. It is essential to validate the code/tool and ensure the data is properly formatted before converting it to JSON format

Conclusion:

Examine potential scenarios contributing to the “JsonParseException: Unexpected character” error. Try all the troubleshooting steps discussed above to resolve the issue.

Happy Learning !!

Jerry Richard
Follow me
Latest posts by Jerry Richard (see all)

Was this post helpful?

Yes
No
Thanks for your feedback!

Leave a Comment