Remove BOM From JSON

Sometimes JSON appears perfectly valid but still fails when parsed. A common reason is the presence of a hidden character called the Byte Order Mark (BOM).

What Is BOM?

The Byte Order Mark is a Unicode character:

U+FEFF

It is sometimes inserted automatically by text editors or file encoders when saving files in UTF-8 format.

Why BOM Breaks JSON

The JSON specification allows only a small set of whitespace characters. When a BOM appears before the JSON object, many parsers throw an error.

SyntaxError: Unexpected token  in JSON

This happens because the parser encounters an invisible character before the opening brace.

Example Problem

{"name":"admin"}

The hidden character before the brace is the BOM.

How To Fix It

You can remove the BOM programmatically:


function removeBOM(input) {
  return input.replace(/\uFEFF/g, "");
}

However, debugging invisible characters manually is difficult.

Easy Solution

Use the Unicode Cleaner tool to detect and remove hidden characters instantly.

Conclusion

The UTF-8 BOM is invisible but can cause major issues in JSON parsing. Detecting and removing hidden Unicode characters ensures reliable parsing and cleaner data pipelines.

Related Guides