fjordtools / Errors / CSV: different number of fields

CSV: row has a different number of fields

Every row in a CSV must have as many values as the first row. When one row has more or fewer, the converter stops and names the row, because it cannot tell which column a value belongs to. The usual cause is a comma inside a value that is not wrapped in quotes.

Fjordtools team · Seen in CSV files from spreadsheets, databases and exports · Published · 1 min read

Common causes

  1. A comma inside a value

    The reader sees Oslo and Norway as two values, so Ann's row has three where the header has two.

    Input

    name,city
    Ann,Oslo, Norway
    Bob,Bergen

    CSV JSON says

    Row 2 has 3 fields but row 1 has 2 (line 2). There is an extra delimiter, or a value with a delimiter in it is not quoted
  2. A missing value

    A row that stops early is one value short. If Ann has no city, write the comma anyway so the empty value is there.

    Input

    name,city
    Ann
    Bob,Bergen

    CSV JSON says

    Row 2 has 1 field but row 1 has 2 (line 2). A field is missing, or a delimiter is inside a value that is not quoted
  3. A trailing comma

    A comma at the end of a row starts one more value, even if nothing follows it.

    Input

    name,city
    Ann,Oslo,
    Bob,Bergen

    CSV JSON says

    Row 2 has 3 fields but row 1 has 2 (line 2). There is an extra delimiter, or a value with a delimiter in it is not quoted
  4. The value in quotes

    Quotes keep the comma inside the value, and every row has two values again.

    Input

    name,city
    Ann,"Oslo, Norway"
    Bob,Bergen

    CSV JSON says

    [{"name":"Ann","city":"Oslo, Norway"},{"name":"Bob","city":"Bergen"}]

How to fix it

  1. Wrap any value that contains the delimiter in double quotes: "Oslo, Norway".
  2. Give every row the same number of delimiters. Write an empty value as nothing between two commas.
  3. Remove a trailing delimiter, or add a header name for the extra column.
  4. If values contain many commas, export with a tab or semicolon as delimiter and choose the same one in the converter.
  5. Paste the text into the CSV JSON converter: the message names the row and line that does not fit.

Related errors

More error explainers are coming

See all errors.