DATA REPRESENTATION CHECKS

Checking a byte-order mark at the start of a CSV header

A byte-order mark at the start of a CSV file can become part of the first column name. This guide shows how to see it and why exact-name matches fail because of it.

A character you cannot see

The code point U+FEFF is called the zero width no-break space, and it also serves as the byte-order mark, or BOM. It has no visible shape. When a file begins with it, a text editor usually hides it and a spreadsheet may skip it. A converter that follows the text exactly keeps it as a normal character. If the file is a CSV with a header row, that first character sits in front of the first column name. The header then reads as the invisible mark followed by id, and it is a different string from id alone, even though the two print the same. Programmers often meet this as a puzzling bug: the header printed in a console looks like id, a string comparison with the literal id returns false, and the length of the string is three instead of two. Nothing about the visible text hints at the cause.

Making the extra code point visible

The Unicode converter in encode mode lists the code points of the text. For the header made of the BOM followed by the letters i and d, it returns U+FEFF U+0069 U+0064. Three code points, where a plain id would have two: U+0069 and U+0064. The leading U+FEFF is the extra one. This listing is the quickest way to prove that a string carries a hidden character, since nothing on screen shows it, and the notation names it exactly. The same listing works for any suspect string: encode the header, count the entries, and compare with the number of letters you can see. A difference of one means one hidden character, and its position in the list shows where it sits. Here it is the first entry, which is the typical place for a BOM.

What the CSV converter does with it

Given the BOM, id, a line break and the value 7, with the header option on, the CSV to JSON converter produces one object. Its only property name is the BOM followed by id, and its value is 7. The JSON output shows the property with the invisible mark inside the quotes, so a lookup for the key id in the result will not find it. The converter preserves the character because removing it would be a decision about the data, and the tool reports what was supplied. If the data row had several columns, only the first property name would carry the mark, for example the mark plus id next to a clean name and a clean email. That uneven pattern is typical of a BOM and different from a file where every header has been altered in the same way.

Where a BOM comes from

In UTF-8, the BOM is the three bytes EF BB BF at the start of a file. The Unicode standard does not require it for UTF-8, since UTF-8 has no byte order to signal, but some programs add it as a marker that the text is UTF-8. Spreadsheet software that offers a CSV UTF-8 export commonly writes it so that other programs open the file with the right encoding. In UTF-16 and UTF-32, the mark does serve its original purpose and shows whether the bytes are big-endian or little-endian. In UTF-16 the pair FE FF means big-endian and FF FE means little-endian. Because the mark is U+FEFF, reading it the wrong way round produces U+FFFE, which the Unicode standard reserves as a noncharacter for exactly this reason, so a reader can tell which order is in use.

Counting the extra bytes

The UTF-8 form of U+FEFF takes three bytes. A header of just id is 2 bytes, and with the mark in front it is 5. The byte length calculator confirms the difference. When a file has been through a tool that adds the mark, its size is three bytes larger than expected, and a checksum differs from the one of the unmarked file. These small differences are often the first sign that an invisible character has been added somewhere in a pipeline.

Typical symptoms

The usual symptom is that a column named id cannot be found, even though the header looks right. Code that reads row.id gets nothing, a database import complains about an unknown column, or a join on the first column returns no matches. The problem affects only the first column, because the mark sits only at the very start of the file. The rest of the header row is fine. That pattern, where one column fails and the others work, is a strong hint to look for a BOM before suspecting the data. A second hint is that the problem appears only for files that came from a particular export path, such as a spreadsheet saved in its UTF-8 CSV option, while files from other sources read normally.

Decide the cleanup rule yourself

The converter does not decide whether the mark should be removed for a destination system. Some readers expect it and some choke on it. The sensible approach is to preserve the source and define the expected header before applying any transformation. If the receiving system wants plain names, strip the leading U+FEFF explicitly in a documented step. If it expects UTF-8 with a signature, keep it. Either way, make the rule visible, so the next person who handles the file does not have to rediscover it from a failed match.

Other invisible characters to check

U+FEFF is not the only character that hides in headers. A non-breaking space, U+00A0, looks like a space and is not equal to one. A zero width space, U+200B, and a zero width joiner, U+200D, are also invisible. Copying a header from a web page or a word processor can bring any of these along. The same code point listing exposes them all, so when an exact-name match fails and the text looks identical, encode both strings and compare the lists.

Byte order marks inside a field

The example concerns a header record that begins with U+FEFF. A mark in the middle of a field is a different matter, since there it is just a zero width no-break space and is kept in the value. The converter does not detect encodings from the bytes of a file, because it works on the text you provide. Once the text is pasted, what you see in the code point list is what the converter works with, and an invisible character explains many failed exact-name matches. For a row of several headers, encode only the first name when the others behave: a mark found there and nowhere else matches the BOM pattern exactly, and the fix then concerns the start of the file, not each column.

Reproduce the checks

Header code points

Open the Unicode Converter and enter:

Text or encoded input
id
Calculation mode
encode

Expected output:

Code points

U+FEFF U+0069 U+0064

The leading code point is visible in notation.

Preserved first property name

Open the Csv To Json Converter and enter:

CSV input
id
7
First row contains headers
true

Expected output:

Rows

[
  {
    "id": "7"
  }
]

The first property includes the leading character.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks