Importing JSON from files adds extra characters, resulting in JsonParseException
Given the following JSON file
Java import this file and adds an extra character right at the beginning
This results in the following error when that JSON is parsed:
Short answer solution
Replacing the first occurrence of the extra added string with a method such as the following should fix this issue:
If we attempt to modify the “cleanUpJsonBOM()” method to replace the extra character with something else, such as “HEREIAM”:
We will then get the following string:
There’s our culprit. Without replacing this bytecode, the “\ufeff” characters would not be visible in debug mode, only within the stacktrace. It is only after replacing these characters that we can detect where the extra BOM was added.
The root of the problem lies in added byte order marks at the beginning of text streams, which are used to signal needed properties to the text stream consumer.
Fortunately, Mark S. Kolich detected this problem with .XML files, which provided a clue towards solving the issue in this post.