Is there a way to remove the BOM from a UTF-8 encoded file?
I know that all of my JSON files are encoded in UTF-8, but the data entry person who edited the JSON files saved it as UTF-8 with the BOM.
When I run my Ruby scripts to parse the JSON, it is failing with an error.
I don't want to manually open 58+ JSON files and convert to UTF-8 without the BOM.
Best Answer
With ruby >= 1.9.2 you can use the mode
r:bom|utf-8
This should work (I haven't test it in combination with json):
It doesn't matter, if the BOM is available in the file or not.
Andrew remarked, that
File#rewind
can't be used with BOM.If you need a rewind-function you must remember the position and replace
rewind
withpos=
: