STRUCTURED TEXT

Diagnosing a CSV row with too few fields

Every record in a CSV file is expected to have the same number of fields as the header. This worksheet shows what a record with a missing field looks like to the parser and how a trailing comma changes the count.

The width rule behind header mode

When header mode is on, the first row names the columns, and each later record is turned into an object with one property per name. That mapping only works if every record supplies a value for every column. RFC 4180, the common reference for CSV, says that each line should contain the same number of fields throughout the file. The converter enforces it, because a short record would force it to invent a value for a column that the source never filled. Rejecting the record puts the problem in front of you, where a silent guess would hide it inside the converted data. With name,color as the header, a record holding red alone leaves the color property with nothing to hold.

Reading the rejected record

The input has a header of name,color and a data row that reads only red. The header defines two fields and the data row holds one, so the converter stops and reports that the records must have the same number of fields. The text of the row looks plausible on its own, which is the trap: red is a perfectly good color name, and nothing about it signals that a second value is missing. The mismatch becomes visible only when the row is held against the header. The page gives no partial output, so you need to repair the source before converting. An error with no result is the correct response here. Returning the first records and dropping the bad one could pass unnoticed into a report or an import, and the missing row would only be discovered later by someone counting.

What the trailing comma does

Change the row to red, followed by a comma and nothing else. A comma ends a field, so the parser now sees two fields on that row: red, and an empty string after the comma. The widths match the header, and the converter returns one object where name is red and color is an empty string. The only difference between the rejected input and the accepted one is a single character. That is why width problems are easy to cause by an editor that trims trailing delimiters, and easy to fix once you know to look for them. A row that should end with three empty columns needs three trailing commas, so a count of delimiters per line is a fast way to compare a suspect line with a healthy one.

Empty is a value, missing is a gap

The object with color set to an empty string is a complete record, but it states something stronger than the short row did. An empty field says the column exists and holds no text. A short row says nothing about the column at all. Those can mean different things in a business setting: a customer with no stated color preference, a color that was never collected, a value lost when the file was edited. The converter can only report what the syntax shows, so adding the comma fixes the structure and leaves the meaning to be settled from the source.

Finding the short row in a longer file

In a file with thousands of rows, a width error can be hard to locate by eye. Convert with header mode off, which accepts rows of different widths, and look at the output for inner arrays with fewer entries than the first one. Spreadsheet software can also help: sort or filter on the last column and look for blanks that sit one column too early. Count the delimiters on suspicious lines with a text editor's search. A value that contained a comma but lost its quotes produces the opposite problem, a row that is too wide, and it is found in the same way.

Common causes of uneven rows

Several habits produce short records. A person deleting the last cell of a row in a plain text editor can remove the delimiter with it. A script that joins fields with commas may skip empty values entirely instead of writing them as empty, shifting later values left. Exports from systems that leave trailing columns off when they are blank do the same. Concatenating files that have different column sets can leave some rows narrower. In each case the repair is to write the missing delimiters, not to change the header.

Repairing without inventing a value

To fix a short record, first decide what the missing field should contain by checking the source of the data. If the answer is that the column is blank for that row, add the empty field with a trailing comma. If the value exists elsewhere, enter it. If you cannot tell, leave the issue open and ask the owner of the file, since inserting zero, none or n/a is a choice about meaning that the file does not support by itself. The converted data will look complete either way, so the decision deserves to be recorded.

Worked pair for testing other files

The pair on this page works as a quick test for any parser or import tool. Feed it name,color with a row of red, and note whether it errors, pads or crashes. Then feed it the version with the trailing comma and confirm that the empty color arrives as an empty string and not as the word null or as a missing property. Tools differ in these choices, and knowing how yours behaves prevents surprises when a real file contains a short row. A successful conversion still does not confirm that the columns match a schema.

When header mode is off

With header mode off the converter returns arrays instead of objects, and the width rule is the only structure it checks for. The same short row then shows up as an inner array holding a single element next to an array of two elements. Because the output is plain arrays, a downstream program that indexes the second element of every row will fail on the short one with an out-of-range read or an undefined value. Using header mode on is therefore the better first test when the file is meant to be rectangular, since the converter catches the problem before any program reads the result. Use the array view when you need to see where the widths diverge. The two-field header here makes the comparison easy: any inner array with a length other than two stands out.

Reproduce the checks

A shorter record

Open the Csv To Json Converter and enter:

CSV input
name,color
red
First row contains headers
true

Expected rejection:

same number of fields

The second field is absent from the record.

An explicit empty second field

Open the Csv To Json Converter and enter:

CSV input
name,color
red,
First row contains headers
true

Expected output:

Rows

[
  {
    "name": "red",
    "color": ""
  }
]

The syntax supplies two fields, one with empty content.

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